网络爬虫-liunx环境下部署selenium+chromedriver

由于一些万恶的网站需要加载js才能抓取数据,我们只能使用selenium去抓取,我们知道selenium如何在windows上跑,只需要将正确对应版本的chromedriver放在Python路径下或者配置PATH环境变量即可。
但是在linux环境下配置selenium+chrome需要配置的环境就很麻烦了,网上的教程也不太全面,我将我花了一天时间查阅的各种资料以及各种报错整理下来,供大家参考。

chrome浏览器请自行面向百度下载。
chromedriver:
国内: 下载地址
国外: 下载地址

linux版本: centos 7.+
chrome浏览器版本:67.0.3396.79-1.el7 
chromedriver版本: 对应chrome浏览器 2.40
Python版本: Python 3.6.5

附上最新chrome对应的driver版本

chromedriver版本 支持的Chrome版本
v2.41 v67-69
v2.40 v66-68
v2.39 v66-68
v2.38 v65-67
v2.37 v64-66
v2.36 v63-65
v2.35 v62-64
v2.34 v61-63
v2.33 v60-62
v2.32 v59-61
v2.31 v58-60
v2.30 v58-60
v2.29 v56-58
v2.28 v55-57
v2.27 v54-56
v2.26 v53-55
v2.25 v53-55
v2.24 v52-54
v2.23 v51-53
v2.22 v49-52
v2.21 v46-50
v2.20 v43-48
v2.19 v43-47
v2.18 v43-46
v2.17 v42-43
v2.13 v42-45
v2.15 v40-43
v2.14 v39-42
v2.13 v38-41
v2.12 v36-40
v2.11 v36-40
v2.10 v33-36
v2.9 v31-34
v2.8 v30-33
v2.7 v30-33
v2.6 v29-32
v2.5 v29-32
v2.4 v29-32

安装chromeium:

# 安装最新的chrome浏览器
[root@izwz97vf52xuyjdkqfsw1oz ~]# yum install -y chromium

安装chromedriver:

# 安装unzip
[root@izwz97vf52xuyjdkqfsw1oz ~]# yum install -y unzip zip
# 将下载好的chromedriver解压
[root@izwz97vf52xuyjdkqfsw1oz ~]# unzip chromedriver_linux64.zip
# 将解压后的chromedriver移至/usr/bin/
[root@izwz97vf52xuyjdkqfsw1oz ~]# mv chromedriver /usr/bin

安装xvf8:

# 安装虚拟GUI--xvf8
[root@izwz97vf52xuyjdkqfsw1oz ~]# yum install Xvfb -y
[root@izwz97vf52xuyjdkqfsw1oz ~]# yum install xorg-x11-fonts* -y 

新建在/usr/bin/ 一个名叫 xvfb-chrom 的文件写入以下内容:

[root@izwz97vf52xuyjdkqfsw1oz ~]# vim /usr/bin/xvfb-chrom

#!/bin/bash  

_kill_procs() {  
  kill -TERM $chromium  
  wait $chromium  
  kill -TERM $xvfb  
}  

# Setup a trap to catch SIGTERM and relay it to child processes  
trap _kill_procs SIGTERM  

XVFB_WHD=${XVFB_WHD:-1280x720x16}  

# Start Xvfb  
Xvfb :99 -ac -screen 0 $XVFB_WHD -nolisten tcp &  
xvfb=$!  

export DISPLAY=:99  

chromium --no-sandbox --disable-gpu$@ &  
chromium=$!  

wait $chromium  
wait $xvfb  

更改权限(重要):

[root@izwz97vf52xuyjdkqfsw1oz ~]# chmod +x chromedriver
[root@izwz97vf52xuyjdkqfsw1oz ~]# chmod +x xvfb-chrom

更改软连接:

[root@izwz97vf52xuyjdkqfsw1oz ~]# ln -s /usr/lib64/chromium-browser/chromium-browser.sh /usr/bin/chromium
[root@izwz97vf52xuyjdkqfsw1oz ~]# rm -rf /usr/bin/chromium-browser
[root@izwz97vf52xuyjdkqfsw1oz ~]# ln -s /usr/bin/xvfb-chromium /usr/bin/chromium-browser
[root@izwz97vf52xuyjdkqfsw1oz ~]# ln -s /usr/bin/xvfb-chromium /usr/bin/google-chrome
[root@izwz97vf52xuyjdkqfsw1oz ~]# ll /usr/bin/ | grep chrom*

OK 大功告成 尝试一下是否能正常运行

[root@izwz97vf52xuyjdkqfsw1oz ~]# vim test1.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--disable-gpu')
chrome_options.add_argument('--disable-dev-shm-usage')
driver = webdriver.Chrome(executable_path=(r'/usr/bin/chromedriver'), chrome_options=c    hrome_options)
driver.get('https://www.baidu.com')
print(driver.title)
driver.quit()

[root@izwz97vf52xuyjdkqfsw1oz ~]# python3 test1.py
百度一下,你就知道

报错问题

错误一:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=xxxxxx (36222509aa6e819815938cbf2709b4849735537c),platform=Linux 4.10.0-42-generic x86_64)

解决方案:

请查看chrome浏览器对应的chromedriver是否正确
windows下载的chromedriver.zip文件请直接用scp传输到liunx系统下之后用unzip解压再放入/usr/bin下

错误二:

selenium.common.exceptions.WebDriverException: Message: unknown error: Chrome failed to start: exited abnormally (Driver info: chromedriver=xxxxxx (36222509aa6e819815938cbf2709b4849735537c),platform=Linux 4.10.0-42-generic x86_64)

解决方案:

记得给chromedriver赋予权限 --> chmod +x /usr/bin/chromedriver
给chrome赋予参数
from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument("--no-sandbox")
chrome_options.add_argument('--disable-gpu')

错误三:

selenium WebDriverException: Message: unknown error: DevToolsActivePort file doesnt exist

解决方案:

1.禁用sandbox
#加上下面两行,解决报错
chrome_options.add_argument('--no-sandbox')
 chrome_options.add_argument('--disable-dev-shm-usage')
chrome_options.add_argument('window-size=1920x3000') #指定浏览器分辨率
chrome_options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
chrome_options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
chrome_options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
chrome_options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败

driver=webdriver.Chrome(chrome_options=chrome_options)
#executable_path驱动路径

2.查看chromedriver是否赋予权限 chmod +x /usr/bin/chromedriver

若还遇到其他报错问题 可留言到我博客下面

猜你喜欢

转载自blog.csdn.net/qq_39802740/article/details/82015197