python - 如何在加载次数有限的页面末尾滚动?硒 - 蟒蛇
问题描述
事实是使用这个:
# # Get scroll height after first time page load
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
# Wait to load page
time.sleep(2)()
# 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
不起作用。是的,它应该适用于具有无限负载的页面,但不适用于 yahoo Finance,它的负载数量有限,但当它到达末尾时,条件应该会中断。所以我现在很困惑。
我们还可以使用:
while driver.find_element_by_tag_name('tfoot'):
# Scroll down three times to load the table
for i in range(0, 3):
driver.execute_script("window.scrollBy(0, 5000)")
time.sleep(2)
但它有时会在特定负载下阻塞。
最好的方法是什么?
解决方案
需要pip install undetected-chromedriver
,但会完成工作。这只是我选择的网络驱动程序,你也可以用普通的硒做同样的事情。
from time import sleep as s
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
import undetected_chromedriver as uc
options = uc.ChromeOptions()
options.headless = False
driver = uc.Chrome(options=options)
driver.get('https://fr.finance.yahoo.com/quote/GM/history?period1=1290038400&period2=1612742400&interval=1d&filter=history&frequency=1d&includeAdjustedClose=true')
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, '#consent-page > div > div > div > div.wizard-body > div.actions.couple > form > button'))).click() #clicks the cookie warning or whatever
last_scroll_pos=0
while True:
WebDriverWait(driver, 10).until(EC.presence_of_element_located((By.CSS_SELECTOR, 'body'))).send_keys(Keys.DOWN)
s(.01)
current_scroll_pos=str(driver.execute_script('return window.pageYOffset;'))
if current_scroll_pos == last_scroll_pos:
print('scrolling is finished')
break
last_scroll_pos=current_scroll_pos
推荐阅读
- nativescript - how do I use SFAuthenticationSession from nativescript?
- visual-studio - 如何以编程方式将现有项目添加到 Visual Studio 中的解决方案文件夹
- php - 使用 curl API 时谷歌驱动器上的无标题文件问题
- java - 如何在最新的安卓手机上运行后台工作,如闹钟
- perforce - Perforce:无法使用描述创建新的更改列表
- javascript - 如何使用 javascript 替换以前的 URL 参数?
- scala - Maven 的插件和依赖项与 SBT 的
- javascript - 有没有办法在javascript中捕获网页搜索词(CTRL + F)?
- android - 将 Android Studio 更新到 3.3.1 后无法启动应用
- react-native - 无法在 React-native 中将调试器附加到 chrome,出现未经授权的错误