嗨,我正在使用 Python Selenium Webdriver 來獲得 Youtube 標題,但不斷獲得比我想要的更多的資訊。該行是:
driver.find_element_by_class_name("style-scope ytd-video-primary-info-renderer").text
有什么方法可以修復它并使其更有效率,以便它只顯示標題。這是我使用的測驗腳本:
from selenium import webdriver as wd
from time import sleep as zz
driver = wd.Firefox(executable_path=r'./geckodriver.exe')
driver.get('https://www.youtube.com/watch?v=wma0szfIafk')
zz(4)
test_atr = driver.find_element_by_class_name("style-scope ytd-video-primary-info-renderer").text
print(test_atr)
uj5u.com熱心網友回復:
要列印標題文本,OBI-WAN KENOBI Official Trailer (2022) Teaser您可以使用以下任一定位器策略:
使用
css_selector和get_attribute("innerHTML"):print(driver.find_element(By.CSS_SELECTOR, "h1.title.style-scope.ytd-video-primary-info-renderer > yt-formatted-string.style-scope.ytd-video-primary-info-renderer").get_attribute("innerHTML"))使用
xpath和文本屬性:print(driver.find_element(By.XPATH, "//h1[@class='title style-scope ytd-video-primary-info-renderer']/yt-formatted-string[@class='style-scope ytd-video-primary-info-renderer']").text)
理想情況下,你需要引起WebDriverWait的visibility_of_element_located(),你可以使用以下的定位策略:
使用
CSS_SELECTOR和文本屬性:print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "h1.title.style-scope.ytd-video-primary-info-renderer > yt-formatted-string.style-scope.ytd-video-primary-info-renderer"))).text)使用
XPATH和get_attribute("innerHTML"):print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//h1[@class='title style-scope ytd-video-primary-info-renderer']/yt-formatted-string[@class='style-scope ytd-video-primary-info-renderer']"))).get_attribute("innerHTML"))注意:您必須添加以下匯入:
from selenium.webdriver.support.ui import WebDriverWait from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC控制臺輸出:
OBI-WAN KENOBI Official Trailer (2022) Teaser
您可以在How toret the text of a WebElement using Selenium - Python 中找到相關討論
參考
鏈接到有用的檔案:
get_attribute()方法Gets the given attribute or property of the element.text屬性回傳The text of the element.- 使用 Selenium 的 text 和 innerHTML 之間的區別
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/365964.html
標籤:Python 硒 硒网络驱动程序 YouTube 网络驱动程序
