我试图使用Python在哈希标签下抓取tweet,并使用下面的代码向下滚动driver.execute_script('window.scrollTo(0,document.body.scrollHeight);')
问题是selinum只会刮掉显示的tweet(只有3条tweet),然后向下滚动到页面的末尾,加载更多的tweet,并刮掉3条新的tweet,中间少了很多tweet。
有没有一种方法可以显示所有的tweet,然后向下滚动显示所有新的tweet,或者至少有一些新的tweet(我有一种机制来过滤已经刮过的卢比)?
注意,我正在GCP上运行我的脚本,所以我不能旋转屏幕。
我想我可以让脚本一直按下箭头,这样我就可以一个一个地显示推文,刮掉它们,还可以继续加载更多的推文,但我认为这会大大降低刮板速度。
发布于 2022-12-01 02:13:24
按像素向下滚动页面,因此页面将获得加载数据的时间,尝试以下代码:
last_height = driver.execute_script("return document.body.scrollHeight")
while True:
driver.execute_script("window.scrollBy(0, 800);") # you can increase or decrease the scrolling height, i.e - '800'
sleep(1)
new_height = driver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height发布于 2022-12-01 09:56:31
要在selenium中向下滚动页面,我们需要编写
driver.execute_script(
"window.scrollTo(" + str(data.location["x"]) + ", " + str(data.location["y"]) + ")")这里的数据是我们得到的tweet
https://stackoverflow.com/questions/74635176
复制相似问题