【发布时间】:2017-11-29 07:05:31
【问题描述】:
我在 Python 中使用带有 selenium 的 Firefox gecko 驱动程序来抓取论坛帖子标题,但遇到了一个我似乎无法弄清楚的问题。
~$ geckodriver --version
geckodriver 0.19.0
The source code of this program is available from
testing/geckodriver in https://hg.mozilla.org/mozilla-central.
This program is subject to the terms of the Mozilla Public License 2.0.
You can obtain a copy of the license at https://mozilla.org/MPL/2.0/.
我正在尝试从论坛上刮掉几年前的帖子标题,我的代码在一段时间内可以正常工作。我坐下来看着它运行了大约 20-30 分钟,它完全按照它应该做的事情做。然而,随后我启动脚本,然后上床睡觉,当我第二天早上醒来时,我发现它已经处理了大约 22,000 个帖子。我目前正在抓取的网站每页有 25 个帖子,因此它在崩溃之前通过了大约 880 个单独的 URL。
当它崩溃时会抛出以下错误:
WebDriverException: Message: Tried to run command without establishing a connection
最初我的代码如下所示:
FirefoxProfile = webdriver.FirefoxProfile('/home/me/jupyter-notebooks/FirefoxProfile/')
firefox_capabilities = DesiredCapabilities.FIREFOX
firefox_capabilities['marionette'] = True
driver = webdriver.Firefox(FirefoxProfile, capabilities=firefox_capabilities)
for url in urls:
driver.get(url)
### code to process page here ###
driver.close()
我也试过了:
driver = webdriver.Firefox(FirefoxProfile, capabilities=firefox_capabilities)
for url in urls:
driver.get(url)
### code to process page here ###
driver.close()
和
for url in urls:
driver = webdriver.Firefox(FirefoxProfile, capabilities=firefox_capabilities)
driver.get(url)
### code to process page here ###
driver.close()
我在所有 3 个场景中都遇到了同样的错误,但只是在它成功运行了一段时间之后,我不知道如何确定它失败的原因。
在成功处理数百个 url 后,如何确定为什么会出现此错误?还是有一些我没有遵循 Selenium/Firefox 处理这么多页面的最佳实践?
【问题讨论】:
标签: python-2.7 selenium selenium-webdriver geckodriver firefox-marionette