使用Python爬虫爬取数据时,需要用到selenium,在服务器上运行时,需要如下配置:
1、安装谷歌浏览器
yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm -y
注:最近使用上面的命令在另外一台机器上安装的时候,报依赖包错误:
Error: Package: google-chrome-stable-126.0.6478.126-1.x86_64 (/google-chrome-stable_current_x86_64)
Requires: libc.so.6(GLIBC_2.25)(64bit)
可以看到最新版的是126版本的,然后尝试更新libc依赖,仍为解决,那就干脆使用上次的118版本,使用如下命令安装:
yum install https://dl.google.com/linux/chrome/rpm/stable/x86_64/google-chrome-stable-118.0.5993.117-1.x86_64.rpm -y
或者也可以结合GitHub - vikyd/chromium-history-version-position: https://vikyd.github.io/download-chromium-history-version/在Chromium History Versions Download ↓这个页面手动下载自己想要的版本,然后再本地安装。
2、安装chromedriver
1)运行下面命令查看浏览器版本
[root@hadoop5 program]# google-chrome --version
Google Chrome 118.0.5993.117
2) 根据谷歌浏览器版本下载对应的浏览器驱动版本
Chrome for Testing availability (这个页面里是高版本的驱动)
ChromeDriver - WebDriver for Chrome - Downloads (这个页面是低版本的驱动)
wget https://edgedl.me.gvt1.com/edgedl/chrome/chrome-for-testing/118.0.5993.70/linux64/chromedriver-linux64.zip
3)解压安装
unzip chromedriver-linux64.zip
mv chromedriver-linux64/chromedriver /usr/bin/
chmod +x /usr/bin/chromedriver
chromedriver -version
3、代码中添加配置
chrome_options.add_argument('--headless')
chrome_options.add_argument('--no-sandbox')
chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--log-level=0')
chrome_options.add_argument('--remote-debugging-port=9222')
# GPU硬件加速
chrome_options.add_argument('–-disable-gpu')
browser = webdriver.Chrome(options=chrome_options)