我正在嘗試撰寫一個刮板,我希望刮板向下滾動,直到它到達“加載更多”按鈕,然后單擊它,但它告訴我不存在這樣的按鈕。這是代碼:
SCROLL_PAUSE_TIME = 5
# Get scroll height
last_height = driver.execute_script("return document.body.scrollHeight")
# Keep scrolling
while True:
# Scroll to bottom
driver.execute_script(
"window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
driver.find_element_by_xpath(
'//button[normalize-space()="LOAD MORE"]').click()
time.sleep(SCROLL_PAUSE_TIME)
while True:
# Scroll to bottom
driver.execute_script(
"window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(SCROLL_PAUSE_TIME)
# Calculate new scroll height and compare with last scroll height
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
這是網址:https ://messari.io/governor/daos?types=Protocol
uj5u.com熱心網友回復:
經過測驗的作業解決方案
import time
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
driver = webdriver.Firefox()
driver.get("https://messari.io/governor/daos?types=Protocol")
def check_exists_by_xpath(xpath):
try:
driver.find_element_by_xpath(xpath)
except NoSuchElementException:
return False
return True
xpath="//*[contains(text(), 'Load More')]"
回圈繼續滾動,直到找到按鈕并暫停 3 秒以加載頁面。
while check_exists_by_xpath(xpath)==False :
time.sleep(3)
driver.execute_script("window.scrollTo(0, document.body.scrollHeight)")
print("scrolling")
print('button successfully found , clicking ')
driver.find_element_by_xpath(xpath).click()
print('finish')
輸出
scrolling
scrolling
scrolling
scrolling
scrolling
scrolling
scrolling
scrolling
scrolling
button successfully found , clicking
finish
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/482283.html
上一篇:Python網頁抓取/資料提取
