我试图运行无头Chrome浏览器,使用Selenium从web上抓取内容。我使用wget安装了无头Chrome,然后解压缩到我当前的文件夹中。
!wget "http://chromedriver.storage.googleapis.com/2.25/chromedriver_linux64.zip"
!unzip chromedriver_linux64.zip
现在,当我加载驱动程序时
from selenium.webdriver.chrome.options import Options
import os
# instantiate a chrome options object so you can set the size and headless preference
chrome_options = Options()
chrome_options.add_argument("--headless")
chrome_options.add_argument("--window-size=1920x1080")
chrome_driver = os.getcwd() +"/chromedriver"
driver = webdriver.Chrome(chrome_options=chrome_options,executable_path=chrome_driver)
我收到一个错误
WebDriverException Traceback (most recent call last)
<ipython-input-67-0aeae0cfd891> in <module>()
----> 1 driver = webdriver.Chrome(chrome_options=chrome_options, executable_path=chrome_driver)
2 driver.get("https://www.google.com")
3 lucky_button = driver.find_element_by_css_selector("[name=btnI]")
4 lucky_button.click()
5 /usr/local/lib/python3.6/dist-packages/selenium/webdriver/chrome/webdriver.py in __init__(self, executable_path, port, chrome_options, service_args, desired_capabilities, service_log_path)
60 service_args=service_args,
61 log_path=service_log_path)
---> 62 self.service.start()
63
64 try:
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in start(self)
84 count = 0
85 while True:
---> 86 self.assert_process_still_running()
87 if self.is_connectable():
88 break
/usr/local/lib/python3.6/dist-packages/selenium/webdriver/common/service.py in assert_process_still_running(self)
97 raise WebDriverException(
98 'Service %s unexpectedly exited. Status code was: %s'
---> 99 % (self.path, return_code)
100 )
101
WebDriverException: Message: Service /content/chromedriver unexpectedly exited. Status code was: -6
更新
所以经过一些研究,我尝试了另一种方法
!apt install chromium-chromedriver
import selenium as se
options = se.webdriver.ChromeOptions()
options.add_argument('headless')
driver = se.webdriver.Chrome(chrome_options=options)
在谷歌Colab上,它再次给了我相同的错误
WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: -6
我已经找到了为什么会出错的问题的答案。请安装chromium chromedriver并将其添加到您的路径变量以及bin目录中。
这是解决如何在Colab上使用Selenium抓取数据问题的成熟解决方案。使用PhantomJS还有一种方法,但Selenium已经否决了这个API,希望他们能在下一次Selenium更新中删除它。
# install chromium, its driver, and selenium
!apt-get update
!apt install chromium-chromedriver
!cp /usr/lib/chromium-browser/chromedriver /usr/bin
!pip install selenium
# set options to be headless, ..
from selenium import webdriver
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
# open it, go to a website, and get results
wd = webdriver.Chrome('chromedriver',options=options)
wd.get("https://www.website.com")
print(wd.page_source) # results
这将适用于任何想在谷歌Colab上而不是在本地机器上抓取数据的人。请按相同顺序依次执行所示步骤。
你可以在这里找到笔记本https://colab.research.google.com/drive/1GFJKhpOju_WLAgiVPCzCGTBVGMkyAjtk。
此错误消息。。。
WebDriverException: Message: Service /content/chromedriver unexpectedly exited. Status code was: -6
意味着ChromeDriver意外退出。
您的主要问题是您使用的二进制文件版本之间存在不兼容性,如下所示:
根据代码行:
!wget "http://chromedriver.storage.googleapis.com/2.25/chromedriver_linux64.zip"
您正在使用chromedriver=2.25
- chromedriver=2.25的发行说明明确提到以下内容:
支持Chrome v53-55
- 虽然您没有提到Chrome浏览器的版本,但预计您使用的是最新的浏览器
因此,ChromeDriver v2.33和最近发布的Chrome浏览器版本之间存在明显的不匹配。
解决方案
- 升级ChromeDriver至当前ChromeDrivev2.44版本
- 将Chrome版本保持在Chrome v69-71级别之间。(根据ChromeDriver v2.44发行说明(
更新
我不确定google-colaboratory
。底线是,相对于安装的谷歌Chrome版本的主流版本,您必须使用ChromeDriver的匹配版本。
但是,您需要先找到在Colab上安装Chrome或Chromium的方法。然后,您可以使用!wget
和!unzip
来下载,解压缩并开始使用匹配的ChromeDriver版本。
您可以在本讨论中找到关于ChromeDriver和Chrome浏览器之间兼容性的讨论
这可能不会直接帮助您。但如果最终无法安装Chrome+selenium,您仍然可以使用phantomjs+seleniun。像这个笔记本:
https://colab.research.google.com/drive/1V62zhjw2V5buxdN1s9mqkLzh3FWqSq8S
但如果可能的话,我更喜欢Chrome。