1. 升级软件
apt-get update
2. 安装pip
apt-get install python3-pip
3. 安装selenium
pip3 install selenium
.4.. 安装chromedriver
sudo apt-get install chromium-chromedriver
5. 查看安装路径
dpkg -L chromium-chromedriver
代码:
from selenium import webdriver from selenium.webdriver.chrome.options import Options chrome_options = Options() # 无头模式启动 chrome_options.add_argument('--headless') chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("window-size=1024,768") chrome_options.add_argument("--no-sandbox") browser = webdriver.Chrome(chrome_options=chrome_options, executable_path="/usr/bin/chromedriver") browser.get("http://www.baidu.com") print(browser.page_source) browser.close()
有时候会出现如下的错误
selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally
(unknown error: DevToolsActivePort file doesn't exist)
(The process started from chrome location /usr/bin/chromium-browser is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
(Driver info: chromedriver=72.0.3626.121,platform=Linux 4.14.71+ armv6l)
原因:
没有添加如下代码 chrome_options.add_argument('--disable-gpu') chrome_options.add_argument("window-size=1024,768") chrome_options.add_argument("--no-sandbox")
截图时汉字变成方块的问题:
安装中文字体
apt-get -y install fontconfig xfonts-utils