前言:
本笔记适合有一定python基础的编程人员查看。
并且在笔记末尾记录了遇到的各种问题以及解决办法
问题:
在本地电脑上写好selenium的爬虫自动化脚本之后,需要放到linux中运行。那在linux_server中应该安装哪些依赖呢?
解决记录:
1、安装谷歌:yum install https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm
2、检查谷歌浏览器的版本:google-chrome --version
3、根据2中的版本,在以下地址中找到最接近我们安装版本的驱动。
安装谷歌浏览器驱动:https://npm.taobao.org/mirrors/chromedriverCNPM Binaries Mirrorhttps://npm.taobao.org/mirrors/chromedriver
最接近的版本需要同时满足以下条件:
从前往后的版本号,能一摸一样的就选一摸一样的.
最后一个版本号,需要大于或者等于驱动的版本号
例如我根据2中查到的结果是:
[root@localhost tmp_delete]# google-chrome --version
Google Chrome 114.0.5735.198
[root@localhost tmp_delete]#
其中114.0.5735.198就是我的环境上的版本号
到驱动下载中查看结果如下
需要确保114.0.5735部分一样,最后的部分找跟198最接近的数字,就找到如下这个版本
点进去之后下载
下载之后的驱动,可以自已存放到任意位置,在代码中指定该驱动的位置即可
额外说明:
需要将selenium设置为无头模式
设置方法为:
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
class Crawl:
def __init__(self):
options = Options()
options.add_argument('--headless')
self.wb = webdriver.Chrome(executable_path='./chromedriver',options=options)
def login(self,username,passwd):
self.wb.find_element(by=By.XPATH,value='//*[@id="userName"]').clear()
# 输入账号
self.wb.find_element(by=By.XPATH,value='//*[@id="userName"]').send_keys(username)
遇到的问题
问题1
selenium.common.exceptions.WebDriverException: Message: 'chromedriver' executable may have wrong permissions. Please see https://sites.google.com/a/chromium.org/chromedriver/home
解决:
给驱动增加可执行权限
chmod +x chromedriver
[root@localhost 114.0.5735.90]# ls -l
总用量 27488
-rw-------. 1 root root 15039112 5月 27 08:10 chromedriver
-rw-------. 1 root root 4013 7月 13 14:55 crawlFromWeb.py
[root@localhost 114.0.5735.90]#
[root@localhost 114.0.5735.90]#
[root@localhost 114.0.5735.90]# chmod +x chromedriver
[root@localhost 114.0.5735.90]#
[root@localhost 114.0.5735.90]#
[root@localhost 114.0.5735.90]#
问题2:
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/google-chrome is no longer running, so ChromeDriver is assuming that Chrome has crashed.)
解决:
options添加参数options.add_argument('--no-sandbox')
options.add_argument('--no-sandbox')