我们有一些 python 脚本可以抓取网站并且运行良好。现在我们想在 Azure Databricks 中执行此操作。 我们认为我们可以通过 Databricks 论坛中的以下帖子来解决此问题,但不幸的是,它不起作用。 ( https://forums.databricks.com/questions/15480/how-to-add-webdriver-for-selenium-in-databricks.html?childToView=21347#answer-21347 )
运行最后一段代码后我们得到的错误是: WebDriverException:消息:未知错误:找不到 Chrome 二进制文件(驱动程序信息:chromedriver=73.0.3683.68(47787ec04b6e38e22703e856e101e840b65afe72),平台=Linux 4.15.0-1050-azure x86_64)
最后一段代码是这样的:
%py
from selenium import webdriver
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_driver = "/tmp/chromedriver/chromedriver"
driver = webdriver.Chrome(chrome_driver,
chrome_options=chrome_options)
driver.get("https://www.google.com")
我找到了一个必须给出二进制文件位置的帖子: Selenium gives "selenium.common.exceptions.WebDriverException: Message: unknown error: cannot find Chrome binary" on Mac
options.binary_location = "/Applications/Google
Chrome.app/Contents/MacOS/Google Chrome"
但我不知道此二进制文件在 Azure Databricks 中的文件位置。
最佳答案
好吧,我在对原始 scipt 做了一个小改动后就可以使用了
%sh /databricks/python3/bin/pip3 install selenium
==================
%sh
wget
https://chromedriver.storage.googleapis.com/73.0.3683.68/chromedriver_linux64.zip
-O /tmp/chromedriver_linux64.zip
==================
%sh mkdir /tmp/chromedriver
================
%sh
unzip /tmp/chromedriver_linux64.zip -d /tmp/chromedriver/
==================
%sh
sudo add-apt-repository ppa:canonical-chromium-builds/stage
===================
%sh
/usr/bin/yes | sudo apt update
===================
%sh
/usr/bin/yes | sudo apt install chromium-browser
===================
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_driver = "/tmp/chromedriver/chromedriver"
driver = webdriver.Chrome(chrome_driver,chrome_options=chrome_options)
driver.get("https://www.google.com")
此脚本将 chromium 下载并更新到版本 77。chromedriver 是 73。更改链接以下载 chromedriver 77 就可以了。
wget
https://chromedriver.storage.googleapis.com/77.0.3865.40/chromedriver_linux64.zip
关于binary - 无法让 selenium webdriver 在 azure databricks 中工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57904372/