web自动化爬虫seleniumcentos无GUI环境配置-成都快上网建站

web自动化爬虫seleniumcentos无GUI环境配置

基本环境:centos7,python3.x

创新互联专注于企业成都全网营销、网站重做改版、建平网站定制设计、自适应品牌网站建设、H5页面制作商城网站建设、集团公司官网建设、外贸网站制作、高端网站制作、响应式网页设计等建站业务,价格优惠性价比高,为建平等各大城市提供网站开发制作服务。

1.安装selenium

pip3 install selenium

2.安装chrome-browser

wget https://dl.google.com/linux/direct/google-chrome-stable_current_x86_64.rpm --no-check-certificate  

yum install ./google-chrome-stable_current_x86_64.rpm

3.下载chromedriver(注意要和chrome-browser版本对应)

web自动化爬虫selenium centos 无GUI环境配置

由于下载的chrome-browser是70版本的,所以chromedriver 选择了2.43版本

wget http://chromedriver.storage.googleapis.com/2.43/chromedriver_linux64.zip

解压此文件,并将文件移动到/usr/bin目录下

unzip chromedriver_linx64.zip

mv chromedriver /usr/bin/

4.测试selenium是否可用,请执行以下python脚本,如返回html内容,则说明安装成功

from selenium import webdriver
url='http://bing.com'
option = webdriver.ChromeOptions()
option.add_argument('--no-sandbox')
option.add_argument('--headless')
driver = webdriver.Chrome(chrome_options=option)
driver.get(url)
print(driver.page_source)

注意需要加上,禁止在沙箱中运行

option.add_argument('--no-sandbox')
option.add_argument('--headless')

补充:

安装firefox支持

yum install firefox

yum install Xvfb

yum install libXfont

pip3 install pyvirtualdisplay

测试脚本

#!/usr/bin/python3
from selenium import webdriver
from pyvirtualdisplay import Display
display = Display(visible=0, size=(800,600))
display.start()
driver = webdriver.Firefox()
driver.get('https://www.baidu.com')
print(driver.title)
driver.quit()
display.stop()

网页题目:web自动化爬虫seleniumcentos无GUI环境配置
本文路径:http://kswjz.com/article/jcedio.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流