python chrome 无GUI centos selenium

centos 安装 chrome 无GUI 情况下

1. 安装Xvfb、google-chrome
2. 测试
3. 本例子使用 
chrome-driver 2.35.528139
google-chrome 70.0.3538.77-1.x86_64

一、安装 google-chrome

  1. 添加 google安装源
    vim /etc/yum.repos.d/google.repo

     [google]name=Google-x86_64baseurl=http://dl.google.com/linux/rpm/stable/x86_64enabled=1gpgcheck=0gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub
    
  2. 安装google chrome

    yum update

    yum install Xvfb -y

    yum install libXfont -y

    yum install xorg-x11-fonts* -y

    yum install google-chrome-stable -y

  3. 测试

    pip install selenium

    pip install pyvirtualdisplay

代码

from selenium import webdriver
executable_path = './chromedriver'
service_log_path ='./chrome.log' 

chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--disable-gpu') # 无
chrome_options.add_argument('–disable-plugins')
chrome_options.add_argument('–disable-java')   ### 禁用java
chrome_options.add_argument('–disable-popup-blocking')  ##  禁止弹出拦截
chrome_options.add_argument('–disable-images') ### 禁止图像加载
chrome_options.add_argument('--verbose')
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-extensions')
chrome_options.add_argument('--no-sandbox') ### 不打开浏览器应用页面
chrome_options.add_argument('–disk-cache-size={}'.format(30*1024*1024))
chrome_options.add_argument('–single-process')
chrome_options.add_argument('–incognito')   ###隐身模式或无痕模式,不保存cookies 缓存文件、历史记录、下架记录
driver = Chrome(chrome_options=chrome_options,
                executable_path=executable_path,
            service_log_path=service_log_path)
driver.get("http://www.baidu.com")
html = driver.page_source
print(html)

driver.quit()

chrome其他参数

chrome_options.add_argument('-user-data-dir={}'.format(user_data_path))  ###user data
chrome_options.add_argument('–disk-cache-dir={}'.format(cache_path))      ###
chrome_options.add_argument('--log-path={}'.format(os.path.join(log,'chrome.log')))
注意:chrome版本与chromedriver版本要一致。不一致式会出错

驱动地址:http://chromedriver.storage.googleapis.com/index.html
版本号对应关系:https://yq.aliyun.com/ziliao/306392

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值