我正在嘗試加載 youtube 頻道的視頻頁面并對其進行決議以提取最近的視頻資訊。我想避免使用 API,因為它有每日使用配額。我遇到的問題是 Selenium 在列印“driver.pagesource”時似乎沒有加載網頁的完整 html:
from bs4 import BeautifulSoup
from selenium.webdriver import Chrome
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.chrome.options import Options
driver = Chrome(executable_path='chromedriver')
driver.get('https://www.youtube.com/c/Oxylabs/videos')
# Agree to youtube cookie popup
try:
consent = driver.find_element_by_xpath(
"//*[contains(text(), 'I agree')]")
consent.click()
except:
pass
# Parse html
WebDriverWait(driver,100).until(EC.visibility_of_element_located((By.XPATH, '//*[@id="show-more-button"]')))
print(driver.page_source)
如上所示,我已嘗試實作 WebDriverWait。這會導致超時例外錯誤。但是,以下 xpath(/html - 網頁末尾)不會導致超時例外:
WebDriverWait(driver,100).until(EC.visibility_of_element_located((By.XPATH, '/html')))
- 但這也不會加載完整的 html。我也嘗試實作 time.sleep(100) 而不是 WebDriverWait,但這也會導致 html 不完整。任何幫助將不勝感激。
uj5u.com熱心網友回復:
您要查找的元素不在頁面上,這是超時的原因:
//*[@id="show-more-button"]
您是否嘗試過滾動到頁面底部或尋找其他元素?
driver.execute_script("arguments[0].scrollIntoView();", element)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/406765.html
標籤:
