原创浮生梦浮生 最后发布于2018-10-06 00:28:41 阅读数 4972  收藏
展开
1. 安装chrome


我使用的是Centos7,使用如下安装方式


配置yum下载源:
在目录 /etc/yum.repos.d/ 下新建文件 google-chrome.repo, 并且在该文件中添加如下内容:


[google-chrome]
name=google-chrome
baseurl=http://dl.google.com/linux/chrome/rpm/stable/$basearch
enabled=1
gpgcheck=1
gpgkey=https://dl-ssl.google.com/linux/linux_signing_key.pub


安装


#yum -y install google-chrome-stable
如果安装失败,可禁用gpg签名验证插件
#yum -y install google-chrome-stable --nogpgcheck


查看版本


# /usr/bin/google-chrome -version


Google Chrome 69.0.3497.100


根据上述版本号查询对应的driver,driver对应可查看博客


https://blog.csdn.net/wudaoshihun/article/details/82353056


 


 


2. 安装chromedriver


mkdir -p /root/webDriver
cd /root/webDriver
wget http://chromedriver.storage.googleapis.com/2.42/chromedriver_linux64.zip
unzip chromedriver_linux64.zip


mv chromedriver /usr/bin/chromedriver


chomod 777 chromedriver


3. 校验


我使用的是配置方式,可直接把路径拷贝代码运行即可
binary_location = '/usr/bin/google-chrome'
chrome_driver_binary= '/usr/bin/chromedriver'


直接拷贝下面程序,引入包,注销
"""
启动函数
"""
代码如下
#coding:utf-8
from selenium import webdriver
import os


binary_location = '/usr/bin/google-chrome'
chrome_driver_binary= '/usr/bin/chromedriver'


def table_crawler_start(pid=0):
        options = webdriver.ChromeOptions()
        options.binary_location = binary_location #谷歌地址
        options.add_argument('--no-sandbox')#解决DevToolsActivePort文件不存在的报错


        options.add_argument('window-size=1920x3000') #指定浏览器分辨率
        options.add_argument('--disable-gpu') #谷歌文档提到需要加上这个属性来规避bug
        options.add_argument('--hide-scrollbars') #隐藏滚动条, 应对一些特殊页面
        options.add_argument('blink-settings=imagesEnabled=false') #不加载图片, 提升速度
        options.add_argument('--headless') #浏览器不提供可视化页面. linux下如果系统不支持可视化不加这条会启动失败
        chromedriver = chrome_driver_binary
        os.environ["webdriver.chrome.driver"] = chromedriver
        brower = webdriver.Chrome(chrome_options=options,executable_path=chromedriver)


        brower.get('https://www.baidu.com')    

        content = brower.page_source.encode('utf-8')
        print content
        #brower.find_element_by_class_name("btn-next").click()
        brower.close()
        brower.quit()
        print("over")


table_crawler_start()





09-03 23:59
查看更多