考虑到垂直爬虫及站内搜索的重要性,重新思考一下项目爬虫的技术架构及实现方案。以前的垂直爬虫曾经使用过heritrix、htmlparser、nutch等,各有优缺点。尤其是要做垂直网站的定向爬取时候,并没有太好的方案,只能够做指定页面的定向解析,因此以前主要还是使用htmlparser的方案。
考察垂直爬虫的几个原则:
厌烦了基于java的爬虫方案,尤其是考虑到python在网络编程上的易用性,因此打算考察基于python做新版本爬虫的可行性,刚好把久不使用的python捡起来。
整理了一下目前基于python的crawler,大致有如下一些现成的项目方案可供参考:
Mechanize:http://wwwsearch.sourceforge.net/mechanize/
Twill:http://twill.idyll.org/
Scrapy:http://scrapy.org
HarvestMan:http://www.harvestmanontheweb.com/
Ruya:http://ruya.sourceforge.net/
BeautifulSoup + urllib2:http://www.crummy.com/software/BeautifulSoup/
比较之后,选择Scrapy作为重点考察学习对象,尽管没有Mechanize及Harvestman成熟,但从其架构来看,还是很有前途的,尤其是基于twisted高性能框架的架构,很有吸引力。
看看Scrapy的架构:
The engine is responsible for controlling the data flow between all components of the system, and triggering events when certain actions occur. See the Data Flow section below for more details.
The Scheduler receives requests from the engine and enqueues them for feeding them later (also to the engine) when the engine requests them.
The Downloader is responsible for fetching web pages and feeding them to the engine which, in turns, feeds them to the spiders.
Spiders are custom classes written by Scrapy users to parse response and extract items (aka scraped items) from them or additional URLs (requests) to follow. Each spider is able to handle a specific domain (or group of domains). For more information see Spiders.
The Item Pipeline is responsible for processing the items once they have been extracted (or scraped) by the spiders. Typical tasks include cleansing, validation and persistence (like storing the item in a database). For more information see Item Pipeline.
Downloader middlewares are specific hooks that sit between the Engine and the Downloader and process requests when they pass from the Engine to the downloader, and responses that pass from Downloader to the Engine. They provide a convenient mechanism for extending Scrapy functionality by plugging custom code. For more information see Downloader Middleware.
Spider middlewares are specific hooks that sit between the Engine and the Spiders and are able to process spider input (responses) and output (items and requests). They provide a convenient mechanism for extending Scrapy functionality by plugging custom code. For more information see Spider Middleware.
Spider middlewares are specific hooks that sit between the Engine and the Scheduler and process requests when they pass from the Engine to the Scheduler and vice-versa. They provide a convenient mechanism for extending Scrapy functionality by plugging custom code.
参考资料:
http://en.wikipedia.org/wiki/Web_crawler#Open-source_crawlers
http://java-source.net/open-source/crawlers
http://chuanliang2007.spaces.live.com/blog/cns!E5B7AB2851A4C9D2!795.entry