简体   繁体   English

来自 Google Colab Selenium 的 Webdriver 错误

[英]Webdriver error from Selenium on Google Colab

I'm creating a scraper on google colab using Selenium but now doesnt work.我正在使用 Selenium 在 google colab 上创建一个抓取工具,但现在不起作用。 Yes in the past but I dont know why now doesn't.过去是的,但我不知道为什么现在没有。

The code is:代码是:

#dependencies
!pip install selenium
!apt-get update 
!apt install chromium-chromedriver
!pip install fake-useragent
from selenium import webdriver
from fake_useragent import UserAgent

#options to chromedriver
ua = UserAgent()
userAgent = ua.random
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome('chromedriver',chrome_options=chrome_options)
chrome_options.add_argument('--user-agent="'+userAgent+'"')

When a I run this code, colab show the next error:当我运行这段代码时,colab 显示下一个错误:

"Message: Service chromedriver unexpectedly exited. Status code was: 1" “消息:服务 chromedriver 意外退出。状态代码为:1”

error screenshot错误截图

Do you know any solution?你知道任何解决方案吗? I've looked for an answer on other related topics but nothing works for me我已经在其他相关主题上寻找答案,但对我没有任何帮助

Run Selenium on Google Colab and scrape organic results在 Google Colab 上运行 Selenium 并抓取有机结果

This error message...这个错误信息...

WebDriverException: Message: Service chromedriver unexpectedly exited. Status code was: 1

...was the result of an issue induced as the colab system was updated from v18.04 to ubuntu v20.04 LTS and with Ubuntu v20.04 LTS no longer distributes chromium-browser outside of a snap package. ...是由于 colab 系统从v18.04更新到 ubuntu v20.04 LTS和 Ubuntu v20.04 LTS LTS 而引起的问题不再在快照 package 之外分发铬浏览器


Solution解决方案

As a solution you can install a compatible version of Chromium without snap following the discussion How to install Chromium without snap?作为一种解决方案,您可以在讨论如何不使用 snap 安装 Chromium 之后安装兼容版本的 Chromium 而无需使用 snap?


Chromium Browser from Debian buster repository来自 Debian buster 存储库的 Chromium 浏览器

However to install chromium-browser from the Debian buster repository using the following code block published by @metrizable in the discussion Issues when trying to use Chromedriver in Colab但是,使用@metrizable在讨论Issues when trying to use Chromedriver in Colab中发布的以下代码块从 Debian buster 存储库安装chromium-browser

%%shell
# Ubuntu no longer distributes chromium-browser outside of snap
#
# Proposed solution: https://askubuntu.com/questions/1204571/how-to-install-chromium-without-snap

# Add debian buster
cat > /etc/apt/sources.list.d/debian.list <<'EOF'
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster.gpg] http://deb.debian.org/debian buster main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-buster-updates.gpg] http://deb.debian.org/debian buster-updates main
deb [arch=amd64 signed-by=/usr/share/keyrings/debian-security-buster.gpg] http://deb.debian.org/debian-security buster/updates main
EOF

# Add keys
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys DCC9EFBF77E11517
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 648ACFD622F3D138
apt-key adv --keyserver keyserver.ubuntu.com --recv-keys 112695A0E562B32A

apt-key export 77E11517 | gpg --dearmour -o /usr/share/keyrings/debian-buster.gpg
apt-key export 22F3D138 | gpg --dearmour -o /usr/share/keyrings/debian-buster-updates.gpg
apt-key export E562B32A | gpg --dearmour -o /usr/share/keyrings/debian-security-buster.gpg

# Prefer debian repo for chromium* packages only
# Note the double-blank lines between entries
cat > /etc/apt/preferences.d/chromium.pref << 'EOF'
Package: *
Pin: release a=eoan
Pin-Priority: 500


Package: *
Pin: origin "deb.debian.org"
Pin-Priority: 300


Package: chromium*
Pin: origin "deb.debian.org"
Pin-Priority: 700
EOF

# Install chromium and chromium-driver
apt-get update
apt-get install chromium chromium-driver

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM