http://blog.csdn.net/cjsafty/article/details/9206323
网页爬虫分为静态网页爬虫和动态网页爬虫,前者是指索要获取的网页内容不需要经过js运算或者人工交互,
后者是指获取的内容必须要经过js运算或者人工交互。这里的js运算可能是ajax,人工交互不需要解释了。
静态爬虫现在已经很成熟了,借助于python中的urllib和beautifulsoup可以很容易实现,爬到的内容通
过python的字符串处理写入数据库,甚至可以通过web形式展现。动态爬虫有两种工具,一种是selenium,现
在是selenium2(selenium+webdriver),另一种是headless的phantomjs(对caperjs的封装),前者主要是
通过控制浏览器实现,尤其是那种带video tag的场合,例如国内的一些CP站点例如youku,后者则是不需要
展现内容的场合,或者可以理解为不带video tag的场合。据说后者的速度要比前者快,因为它不需要浏览器
展现,可以闷头去做。
当然还有其他的工具,这里就不列举了。
本文描述的是基于selenium的,因为我想获取video tag,这个phantomjs是做不到的。
本文分几个部分描述。相关网页地址,安装配置,示例代码。这里假设熟悉python静态爬虫和相关语法(例如HTML等)。
一、相关网页地址:
1,phantomjs:这里给出github地址及其说明。
https://github.com/ariya/phantomjs/wiki/Supported-Web-Standards
2,phantomjs:这里给出一个例子的地址
http://pm163.lofter.com/post/aa404_197d8e
3,js对搜索引擎的影响:
http://www.gooseeker.com/cn/node/knowledgebase/javascriptvsseo
4,selenium项目来源:
http://www.infoq.com/cn/news/2011/06/selenium-arch
5,selenium 文档之一:
http://docs.seleniumhq.org/docs/03_webdriver.jsp
6,基于python的selenium及下载页:
http://selenium-python.readthedocs.org/en/latest/installation.html#introduction
https://pypi.python.org/pypi/selenium
7,基于PYthon的官方文档和非官方文档
http://selenium.googlecode.com/svn/trunk/docs/api/py/index.html
http://selenium-python.readthedocs.org/en/latest/index.html
8,基于chrome的webdriver:这里注意chorme的版本,现在是27.
https://code.google.com/p/selenium/wiki/ChromeDriver
https://code.google.com/p/chromedriver/downloads/list
二,配置
第一部分列举了那么多地址,是因为查阅资料很重要,安装和使用是很快的。
简单的说,如果要基于windows做的,那么chromedriver、python和chrome都要winddows版本,这
里不能用cygwin。因为cygwin是linux,并且可以理解为不带桌面的linux。
如果要基于linux的,那么必须是linux桌面版,我这里用的是ubuntu 桌面版12.04 LTS。这样整套都
在linux下。
webdriver要放在/usr/bin目录下。而不是和chrome同级目录下。后者一般是/opt/google/chrome。
否则会提示找不到webdriver。selenium2的实例是基于firefox的,因为firefox是ubuntu默认带的,所以
天然就可以用。
如果是不带server的方式驱动,而是直接用webdriver驱动,那么python代码要在linux系统里面
的终端去运行,不能通过远程控制方式运行,否则会报错。 server方式远程控制一台机器是另一种配置方法。
三。实例代码:
1,面的实例代码是原生的例子,调用firefox可以直接使用。调用chrome,则直接改成webdriver.Chrome()即可。
- from selenium import webdriver
- from selenium.common.exceptions import NoSuchElementException
- from selenium.webdriver.common.keys import Keys
- import time
- browser = webdriver.Firefox() # Get local session of firefox
- browser.get("http://www.yahoo.com") # Load page
- assert "Yahoo!" in browser.title
- elem = browser.find_element_by_name("p") # Find the query box
- elem.send_keys("seleniumhq" + Keys.RETURN)
- time.sleep(0.2) # Let the page load, will be added to the API
- try:
- browser.find_element_by_xpath("//a[contains(@href,'http://seleniumhq.org')]")
- except NoSuchElementException:
- assert 0, "can't find seleniumhq"
- browser.close()
2,改UA:下面代码是改UA的例子,仅导入chrome_options即可。我列举了chrome里面的四种常见移动设备的UA。
有一个网站可以专门显示UA:chrome只要你改好UA后,用这个网站测试即可。
http://www.whatsmyuseragent.com/
- options = webdriver.ChromeOptions()
- #options.add_argument('--user-agent=Mozilla/5.0 (iPad; CPU OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')
- #options.add_argument('--user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3')
- #options.add_argument('--user-agent=Mozilla/5.0 (Linux; U; Android 2.3.6; en-us; Nexus S Build/GRK39F) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1')
- options.add_argument('--user-agent=Mozilla/5.0 (Linux; U; Android 4.0.2; en-us; Galaxy Nexus Build/ICL53F) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30')
- browser = webdriver.Chrome(chrome_options=options) # Get local session of firefox
3,获取网页内容:
python版本的selenium2获取到的内容自然是python能解析的。
例如某网站下述video tag。
- <video id="xxx-html5player-video" preload="none"
- src="http://xxx.com/xxxx/v.m3u8"
- style="width: 100%; height: 100%; display: none; position: relative;"></video>
- 下面的代码片段即可得到URL
- url=xxx'
- browser = webdriver.Chrome
- browser.get(url)
- elem=browser.find_element_by_id('xxx-html5player-video') #by id
- elem=browser.find_element_by_tag_name('video') #by tag name
- elem.get_attribute('src')#get url,python风格的词典结构,elem返回的是词典结构。