遇见了一些selenium chromium chromedriver环境设置和平台问题,在本地电脑上早就做好了两个Python爬虫,天天要自己手动启动,好烦~就买了台服务器,弄上去每天定时启动,下面是解决的过程和一些资料收集;
1服务器环境是宝塔,需要做selenium chromium chromedriver环境设置:
定时任务部分是单独的PATH环境变量,导致了已经安装好的环境,不生效;需要切换PATH环境之后才生效。
export PATH=xxxxxxxxxxx
2由于谷歌Chrome并不支持linux
由于谷歌chrome并不支持linux, 也就更不支持Centos了,所以需要安装Chromium,不过不用担心,Chromium也是谷歌的开源项目,与Chrome并没有太大的区别安装代码:yum install chromium
Chromium存放路径:
使用which命令查看位置
3下载对应的chromedriver谷歌源 or 淘宝源:
http://chromedriver.storage.googleapis.com/index.html
https://npm.taobao.org/mirrors/chromedriver/
下载对应版本的chromedriver
wget xxxxxxxxxxxxxx
解压
unzip chromedriver_linux64.zip
chromedriver 存放路径:
/user/local/bin
4安装和查看chromium和chromedriver的版本是否一致
chromium –version
chromedriver –version
5指定chromium启动程序路径:
option.binary_location=(r’/usr/bin/chromium-browser’)
6指定启动chromedriver路劲:
browser = webdriver.Chrome(‘/usr/local/bin/chromedriver’,chrome_options=option)
7测试DEMO:
from selenium import webdriveroption = webdriver.ChromeOptions()
option.add_argument('headless')
option.add_argument('no-sandbox')
option.add_argument('disable-dev-shm-usage')
browser = webdriver.Chrome('/usr/local/bin/chromedriver',chrome_options=option)
browser.get('http://www.baidu.com/')
print(browser.title)
exit();
其他尝试项:
安装google chrome
yum -y install google-chrome-stable --nogpgcheck
查看google chrome启动程序路径
which google-chrome-stable
# 返回结果:/usr/bin/google-chrome
建立Chrome软链接
ln -s /usr/bin/google-chrome /bin/chrome
# 这里的路径是通过上一步查询出来的结果
查看进程:
ps -ef | grep chrome
杀掉chrome进程:
killall chromedriver
killall chromium-browser
持续报错:selenium.common.exceptions.WebDriverException: Message: unknown error: DevToolsActivePort file doesn't exist
第一次尝试无效:
option = webdriver.ChromeOptions()
option.add_argument('headless')
option.add_argument('no-sandbox')
option.add_argument('disable-dev-shm-usage')
browser = webdriver.Chrome('/usr/local/bin/chromedriver',chrome_options=option)
第二次尝试修改:
option.binary_location=(r'/usr/bin/chromium-browser')