Home  >  Article  >  Backend Development  >  python爬虫常用的模块分析

python爬虫常用的模块分析

WBOY
WBOYOriginal
2016-06-16 08:42:221452browse

本文对Python爬虫常用的模块做了较为深入的分析,并以实例加以深入说明。分享给大家供大家参考之用。具体分析如下:

creepy模块

某台湾大神开发的,功能简单,能够自动抓取某个网站的所有内容,当然你也可以设定哪些url需要抓。

地址:https://pypi.python.org/pypi/creepy

功能接口:

set_content_type_filter:
设定抓取的content-type(header中的contenttype)。包括text/html

add_url_filter:
过滤url,传入的可以是正则表达式

set_follow_mode:
设定递归模式,F_ANY:该页面上所有链接都会抓取。 F_SAME_DOMAIN和F_SAME_HOST类似。即同一个域名的都会抓取。F_SAME_PATH:同一路径的抓取。例如bag.vancl.com/l1/d3/1.jpg path为l1/d3/1.jpg,则path为l1/d3/*的都会抓取。这里可以根据需要增加自己的递归模式

set_concurrency_level:
设定线程最大数

process_document:
一般需要重写,处理网页内容,提取自己需要的内容。

selenium
可视化界面,抓取自动化,api使用超简单,完全像是自己在操作浏览器。

官方网站:http://www.seleniumhq.org/
python官方网站
http://pypi.python.org/pypi/selenium
webdriver api(很好用,建议多了解一下)
http://www.seleniumhq.org/docs/03_webdriver.jsp

以下是一个抓取凡客网站的例子:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import time

browser = webdriver.Firefox()
browser.get('http://bag.vancl.com/28145-28167-a18568_18571-b1-n3-s1.html#ref=hp-hp-hot-8_1_1-v:n')
elem = browser.find_element_by_name('ch_bag-3-page-next') # Find the search box
time.sleep(1)
print elem.get_attribute("href")
elem.click()

time.sleep(1)
elem = browser.find_element_by_name('ch_bag-3-page-next') # Find the search box
print elem.get_attribute("href")
elem.click()

希望本文所述对大家的Python程序设计有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn