1、安装依赖包
yum install wget firefox gcc zlib zlib-devel Xvfb
2、安装setuptools
官网地址:https://pypi.python.org/pypi/setuptools
百度地址:http://pan.baidu.com/s/1i5JlRH7
tar zxvf setuptools-31.0.0.tar.gz cd setuptools-31.0.0 python setup.py install
3、安装selenium
官网地址:https://pypi.python.org/pypi/selenium#downloads
百度地址:http://pan.baidu.com/s/1miJfI7A
tar zxvf selenium-3.0.2.tar.gz cd selenium-3.0.2 python setup.py install
4、安装PyVirtualDisplay
官网地址:https://pypi.python.org/pypi/PyVirtualDisplay
百度地址:http://pan.baidu.com/s/1gf2uUBL
tar zxvf PyVirtualDisplay-0.2.1.tar.gz cd PyVirtualDisplay-0.2.1 python setup.py install
注意:如果提示EasyProcess相关的错误 ,请先安装EasyProcess再安装PyVirtualDisplay。
EasyProcess下载地址:https://pypi.python.org/pypi/EasyProcess 安装方法和PyVirtualDisplay一样
由于我的CentOS是没有界面的,所以需要安装PyVirtualDisplay来模拟。
5、安装geckodriver
官网地址:https://github.com/mozilla/geckodriver/releases
百度地址:http://pan.baidu.com/s/1i54YH5z
tar zxvf geckodriver-v0.11.1-linux64.tar.gz mv geckodriver /usr/local/bin
6、Python代码
# -*- coding: utf-8 -*- from selenium import webdriver from pyvirtualdisplay import Display display = Display(visible=0, size=(800,600)) display.start() dr = webdriver.Firefox() dr.get('http://www.qq.com/') url = dr.find_element_by_css_selector('#todaytop a').get_attribute('href') dr.get(url) #获取标题 title = dr.find_element_by_id('sharetitle').text print(title) #获取内容 content = dr.find_element_by_id('articleContent').get_attribute('innerHTML') print(content) dr.quit()
执行效果:
其他资料
selenium-webdriver+python基本操作:http://pan.baidu.com/s/1nvCsmBR