gpt4 book ai didi

python - Selenium获取python的所有属性

转载 作者:行者123 更新时间:2023-12-03 16:53:24 25 4
gpt4 key购买 nike

我正在尝试获取页面上的所有元素,但是它具有无限滚动。我尝试向下滚动页面,然后获取属性,但是没有将它们全部拿走吗?由于某种原因,我只能得到其中的一半?

 driver = webdriver.Firefox()
driver.get("http://www.amazon.com/gp/pdp/profile/A2A46BUQRGSAB0/ref=cm_cr_dp_pdp")
lastHeight = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(3)
newHeight = driver.execute_script("return document.body.scrollHeight")
print newHeight, lastHeight
if newHeight == lastHeight:
break
lastHeight = newHeight
tree = etree.HTML(driver.page_source)
product = tree.xpath('//span[@class="a-size-base product-title pr-multiline-ellipses-container"]//text()')[::3]
print len(product)

最佳答案

您需要等待滚动生效。否则,您将在更新完成之前获取源代码。

简单但并非完美的解决方法是在足够的时间内使用time.sleep

import time

driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
time.sleep(1) # <---
newHeight = driver.execute_script("return document.body.scrollHeight")

关于python - Selenium获取python的所有属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31768053/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com