树莓派使用undetected_chromedriver

· 413 words · 1 minute read

undetected_chromedriver使用的chromedriver必须得是arm平台的,而官方没有arm平台的, 最省事的办法就是直接:

sudo apt-get install chromium-chromedriver

找到chromedriver的路径,一般在/usr/bin/chromedriver

sudo find / -name chromedriver

把chromedriver复制到家目录执行,否则没权限执行

cp /usr/bin/chromedriver ~

在装好必备的包以后就要开始写脚本了

pip install undetected_chromedriver
pip install selenium

undetected_chromedriver这个包里的Chrome()方法的参数driver_executable_path是用来设置上面提到的chromedriver的, Chrome()方法的参数version_main是用来设置版本的,不设置会报错

import undetected_chromedriver as uc
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
# “–no-sandbox”参数是让Chrome在root权限下跑
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
# “–headless”参数是不用打开图形界面
chrome_options.add_argument('--headless')
chrome_options.add_argument('blink-settings=imagesEnabled=false')
chrome_options.add_argument('--disable-gpu')

# 使用 Chromium 的 WebDriver
driver_path = "/home/username/chromedriver"
driver = uc.Chrome(driver_executable_path=driver_path, options=chrome_options, version_main=110)

# 开启浏览器
driver.get('https://www.bing.com/')
# 打印网页源码
print(driver.page_source)
# 关闭浏览器
driver.close()