You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Selenium爬取Vimeo页面meta元素无文本结果,如何解决?

问题解决方法

你的核心问题是meta标签的内容并不存储在元素的文本节点中,而是放在content属性里,所以用.text方法获取不到任何内容,和XPath/CSS选择器是否支持无关。

修改后的代码

import selenium
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from bs4 import BeautifulSoup
import time
import pandas as pd


driver = webdriver.Chrome()
url = 'https://vimeo.com/510879223'
driver.get(url)

# 用显式等待确保meta元素加载完成(更可靠)
wait = WebDriverWait(driver, 10)
# 替换成属性选择器,比基于位置的:nth-child更稳定
title = wait.until(EC.presence_of_element_located((By.XPATH, "//meta[@property='og:title']")))
print(title.get_attribute('content'))

description = wait.until(EC.presence_of_element_located((By.XPATH, "//meta[@property='og:description']")))
print(description.get_attribute('content'))

driver.quit()

关键修改点

  • 把.text替换成get_attribute('content'),这是获取meta标签内容的正确方式
  • 用og:title属性选择器替代head > meta:nth-child(14),避免页面结构变动导致定位失效
  • 增加显式等待WebDriverWait,确保元素已经加载完成再进行操作,避免因页面加载延迟导致的问题

补充说明

Vimeo的页面是支持XPath/CSS选择器定位的,你之前能定位到WebElement就证明了这一点,只是获取内容的方式错误。meta标签属于HTML头部的元数据,所有这类标签的内容都需要通过属性来读取,而不是文本节点。

内容的提问来源于stack exchange,提问作者HALEY

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:40:48