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.gzcd setuptools-31.0.0python 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.gzcd selenium-3.0.2python setup.py install
4、安装PyVirtualDisplay
官网地址:https://pypi.python.org/pypi/PyVirtualDisplay
百度地址:http://pan.baidu.com/s/1gf2uUBL
tar zxvf PyVirtualDisplay-0.2.1.tar.gzcd PyVirtualDisplay-0.2.1python 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.gzmv geckodriver /usr/local/bin
6、Python代码
# -*- coding: utf-8 -*- from selenium import webdriverfrom pyvirtualdisplay import Displaydisplay = 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').textprint(title)#获取内容content = dr.find_element_by_id('articleContent').get_attribute('innerHTML')print(content)dr.quit()
执行效果:
其他资料
selenium-webdriver+python基本操作: