【发布时间】:2019-02-04 15:45:44
【问题描述】:
我正在使用 selenium 来抓取无限滚动页面。
我正在尝试使用此代码:
import time
import pandas as np
import numpy as np
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
browser = webdriver.Chrome()
url = 'https://twitter.com/search?f=tweets&q=csubwaystats%20since%3A2018-05-28%20until%3A2018-08-28'
browser.get(url)
time.sleep(1)
SCROLL_PAUSE_TIME = 0.5
# Get scroll height
last_height = webdriver.execute_script("return document.body.scrollHeight")
while True:
# Scroll down to bottom
webdriver.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 = webdriver.execute_script("return document.body.scrollHeight")
if new_height == last_height:
break
last_height = new_height
我从多个来源获得此代码,最近的是:
How can I scroll a web page using selenium webdriver in python?
我将它更新为包含“webdriver”而不是“driver”,因为我将 selenium 作为 webdriver 导入。否则它不起作用。
我的问题是,当我运行代码时,我得到:
AttributeError: module 'selenium.webdriver' has no attribute 'execute_script'
我真的不明白这意味着什么以及如何解决它?我无法找到这方面的信息。
我是 python 新手,所以可能遗漏了一些明显的东西,但任何建议都将不胜感激。
【问题讨论】:
-
请用您使用的确切代码更新问题
-
就这么做了。谢谢!
标签: python selenium selenium-webdriver driver