我试图在这个页面上做无限滚动,下面是我的代码:
from selenium import webdriver
import time
profile = webdriver.FirefoxProfile()
profile.set_preference("general.useragent.override","Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:28.0) Gecko/20100101 Firefox/28.0")
driver = webdriver.Firefox(profile)
driver.get("http://www.quora.com/Programming-Languages/followers")
for n in range(0,5): # For testing I have capped this at 5, will handle this properly once things start to work.
driver.execute_script("window.scrollTo(0,1000000);")
time.sleep(2)因此,当我运行这个程序时,它会等待很多秒(有时超过1分钟),然后再进行任何滚动,然后在下一个滚动之前再次等待相同的时间。该代码在其他页面上似乎运行良好。有什么办法解决这个问题吗?
当我尝试使用Chrome而不是firefox时,我会发现以下错误:driver = webdriver.Chrome('/home/asdf/apps/chromedrive/chromedriver')添加到.py文件中。
Traceback (most recent call last):
File "ok.py", line 8, in <module>
driver = webdriver.Chrome('/home/asdf/apps/chromedrive/chromedriver')
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/chrome/webdriver.py", line 65, in __init__
keep_alive=True)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 73, in __init__
self.start_session(desired_capabilities, browser_profile)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 121, in start_session
'desiredCapabilities': desired_capabilities,
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/webdriver.py", line 171, in execute
response = self.command_executor.execute(driver_command, params)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 349, in execute
return self._request(command_info[0], url, body=data)
File "/usr/local/lib/python2.7/dist-packages/selenium/webdriver/remote/remote_connection.py", line 379, in _request
self._conn.request(method, parsed_url.path, body, headers)
File "/usr/lib/python2.7/httplib.py", line 973, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python2.7/httplib.py", line 1007, in _send_request
self.endheaders(body)
File "/usr/lib/python2.7/httplib.py", line 969, in endheaders
self._send_output(message_body)
File "/usr/lib/python2.7/httplib.py", line 829, in _send_output
self.send(msg)
File "/usr/lib/python2.7/httplib.py", line 791, in send
self.connect()
File "/usr/lib/python2.7/httplib.py", line 772, in connect
self.timeout, self.source_address)
File "/usr/lib/python2.7/socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno -2] Name or service not known发布于 2014-10-08 14:15:49
切换到Chrome()帮助我解决了这个问题:
import time
from selenium import webdriver
followers_per_page = 18
driver = webdriver.Chrome()
driver.get("http://www.quora.com/Programming-Languages/followers")
# get the followers count
element = driver.find_element_by_class_name('count')
followers_count = int(element.text.replace('k', '000').replace('.', ''))
print followers_count
# scroll down the page iteratively with a delay
for _ in xrange(0, followers_count/followers_per_page + 1):
driver.execute_script("window.scrollTo(0, 0,1000000);")
time.sleep(2)FYI,我使用了一些不同的方法:解析追随者的计数和计算每个页面的追随者,同时考虑到它一次加载18个关注者的事实。
实际上,我曾经研究过一个类似的quora问题,参见:
嗯,这不是我脑子里第一件事。故事是这样的。
问题是,对http://tch840195.tch.quora.com/up/chan5-8886/updates URL有一些挂起的请求,这些请求需要几分钟才能完成。这就是selenium认为页面没有被完全加载的原因。而且,事情变得越来越糟--这是一个周期性的事情,每X秒发生一次。把它想成是长时间的汇集。
我已经尝试了多种方法来克服这个问题,使用Firefox webdriver:
webdriver.load.strategy首选项设置为unstablenetwork.http.response.timeout、network.http.connection-timeout、network.http.keep-alive.timeout和network.http.request.max-start-delay首选项window.stop();,希望它能够停止活动请求:
driver.execute_script('window.stop();')另一种可能有效的选择是以某种方式阻止对该“慢速url”的请求,或者使用代理服务器并指向火狐,或者,如果可能的话,让火狐知道将该URL列入黑名单(可能是通过扩展)。
还请参阅内部有多个变通方法的相关问题:
另见:
https://stackoverflow.com/questions/26257960
复制相似问题