整理了Node.js、PHP、Go、JAVA、Ruby、Python等語言的爬蟲框架。不知道讀者們都用過什么爬蟲框架?爬蟲框架的哪些點你覺得好?哪些點覺得不好?

Node.js
- node-crawler

https://github.com/bda-research/node-crawler
Github stars = 3802
北京bda資訊公司數(shù)據(jù)團隊的作品

優(yōu)點:
- 天生支持非阻塞異步IO
- 支持對 DOM 快速選擇
- 符合jQuery語法的選擇器功能(默認使用Cheerio)
- 支持連接池模式,并發(fā)數(shù)和重連數(shù)均可配置
- 支持請求隊列的優(yōu)先權(即不同URL的請求能有不同的優(yōu)先級)
- 支持延時功能(某些服務器對每分鐘內連接數(shù)有限制)
- 支持 forceUTF8 模式以應對復雜的編碼問題,當然你也可以自己為不同的連接設置編碼
關于V8引擎
- 一個完整JavaScript引擎的執(zhí)行過程大致流程如下:源代碼-→抽象語法樹-→字節(jié)碼-→JIT(JIT編譯器)-→本地代碼
- 在V8引擎中,源代碼先被解析器轉變?yōu)槌橄笳Z法樹(AST),然后使用JIT編譯器的全代碼生成器從AST直接生成本地可執(zhí)行代碼。
PHP
- QueryList

https://github.com/jae-jae/QueryList
Github stars = 1016
特點
- 擁有與jQuery完全相同的CSS3 DOM選擇器
- 擁有與jQuery完全相同的DOM操作API
- 擁有通用的列表采集方案
- 擁有強大的HTTP請求套件,輕松實現(xiàn)如:模擬登陸、偽造瀏覽器、HTTP代理等意復雜的網(wǎng)絡請求
- 擁有亂碼解決方案
- 擁有強大的內容過濾功能,可使用jQuey選擇器來過濾內容
- 擁有高度的模塊化設計,擴展性強
- 擁有富有表現(xiàn)力的API
- 擁有高質量文檔
- 擁有豐富的插件
- 擁有專業(yè)的問答社區(qū)和交流群
Go
- Colly

https://github.com/gocolly/colly
Github stars = 5065
Features
- Clean API
- Fast (>1k request/sec on a single core)
- Manages request delays and maximum concurrency per domain
- Automatic cookie and session handling
- Sync/async/parallel scraping
- Distributed scraping
- Caching
- Automatic encoding of non-unicode responses
- Robots.txt support
- Google App Engine support

- Pholcus
https://github.com/henrylee2cn/pholcus
GitHub stars = 4089
支持單機、服務端、客戶端三種運行模式,擁有Web、GUI、命令行三種操作界面;規(guī)則簡單靈活、批量任務并發(fā)、輸出方式豐富(mysql/mongodb/kafka/csv/excel等)、有大量Demo共享;另外它還支持橫縱向兩種抓取模式,支持模擬登錄和任務暫停、取消等一系列高級功能。
框架特點
- 為具備一定Go或JS編程基礎的用戶提供只需關注規(guī)則定制、功能完備的重量級爬蟲工具;
- 支持單機、服務端、客戶端三種運行模式;
- GUI(Windows)、Web、Cmd 三種操作界面,可通過參數(shù)控制打開方式;
- 支持狀態(tài)控制,如暫停、恢復、停止等;
- 可控制采集量;
- 可控制并發(fā)協(xié)程數(shù);
- 支持多采集任務并發(fā)執(zhí)行;
- 支持代理IP列表,可控制更換頻率;
- 支持采集過程隨機停歇,模擬人工行為;
- 根據(jù)規(guī)則需求,提供自定義配置輸入接口
- 有mysql、mongodb、kafka、csv、excel、原文件下載共五種輸出方式;
- 支持分批輸出,且每批數(shù)量可控;
- 支持靜態(tài)Go和動態(tài)JS兩種采集規(guī)則,支持橫縱向兩種抓取模式,且有大量Demo;
- 持久化成功記錄,便于自動去重;
- 序列化失敗請求,支持反序列化自動重載處理;
- 采用surfer高并發(fā)下載器,支持 GET/POST/HEAD 方法及 http/https 協(xié)議,同時支持固定UserAgent自動保存cookie與隨機大量UserAgent禁用cookie兩種模式,高度模擬瀏覽器行為,可實現(xiàn)模擬登錄等功能;
- 服務器/客戶端模式采用Teleport高并發(fā)SocketAPI框架,全雙工長連接通信,內部數(shù)據(jù)傳輸格式為JSON。



JAVA
- webmagic
https://github.com/code4craft/webmagic
Github stars = 6643

webmagic的主要特色:
- 完全模塊化的設計,強大的可擴展性。
- 核心簡單但是涵蓋爬蟲的全部流程,靈活而強大,也是學習爬蟲入門的好材料。
- 提供豐富的抽取頁面API。
- 無配置,但是可通過POJO+注解形式實現(xiàn)一個爬蟲。
- 支持多線程。
- 支持分布式。
- 支持爬取js動態(tài)渲染的頁面。
- 無框架依賴,可以靈活的嵌入到項目中去。

WebMagic的四個組件:
1.Downloader
Downloader負責從互聯(lián)網(wǎng)上下載頁面,以便后續(xù)處理。WebMagic默認使用了Apache HttpClient作為下載工具。
2.PageProcessor
PageProcessor負責解析頁面,抽取有用信息,以及發(fā)現(xiàn)新的鏈接。WebMagic使用Jsoup作為HTML解析工具,并基于其開發(fā)了解析XPath的工具Xsoup。
在這四個組件中,PageProcessor對于每個站點每個頁面都不一樣,是需要使用者定制的部分。
3.Scheduler
Scheduler負責管理待抓取的URL,以及一些去重的工作。WebMagic默認提供了JDK的內存隊列來管理URL,并用集合來進行去重。也支持使用Redis進行分布式管理。
除非項目有一些特殊的分布式需求,否則無需自己定制Scheduler。
4.Pipeline
Pipeline負責抽取結果的處理,包括計算、持久化到文件、數(shù)據(jù)庫等。WebMagic默認提供了“輸出到控制臺”和“保存到文件”兩種結果處理方案。
Pipeline定義了結果保存的方式,如果你要保存到指定數(shù)據(jù)庫,則需要編寫對應的Pipeline。對于一類需求一般只需編寫一個Pipeline。
- crawler4j
https://github.com/yasserg/crawler4j
GitHub stars = 2944
沒有文檔,只有git
優(yōu)點
- 多線程采集
- 內置了Url 過濾機制,采用的是BerkeleyDB 進行url的過濾。
- 可擴展為支持結構化提取網(wǎng)頁字段,可作為垂直采集用
- WebCollector
https://github.com/CrawlScript/WebCollector
GitHub stars = 1883
沒有文檔,只有git
WebCollector是一個無須配置、便于二次開發(fā)的JAVA爬蟲框架,它提供精簡的的API,只需少量代碼即可實現(xiàn)一個功能強大的爬蟲。WebCollector-Hadoop是WebCollector的Hadoop版本,支持分布式爬取。
- Nutch
https://github.com/apache/nutch
GitHub stars = 1703

Features
- Fetching and parsing are done separately by default, this reduces the risk of an error corrupting the fetch parse stage of a crawl with Nutch.
- Plugins have been overhauled as a direct result of removal of legacy Lucene dependency for indexing and search.
- The number of plugins for processing various document types being shipped with Nutch has been refined. Plain text, XML, OpenDocument (OpenOffice.org), Microsoft Office (Word, Excel, Powerpoint), PDF, RTF, MP3 (ID3 tags) are all now parsed by the Tika plugin. The only parser plugins shipped with Nutch now are Feed (RSS/Atom), HTML, Ext, JavaScript, SWF, Tika & ZIP.
- MapReduce ;
- Distributed filesystem (via Hadoop)
- Link-graph database

Nutch的優(yōu)缺點
優(yōu)點:
Nutch支持分布式抓取,并有Hadoop支持,可以進行多機分布抓取,存儲和索引。另外很吸引人的一點在于,它提供了一種插件框架,使得其對各種網(wǎng)頁內容的解析、各種數(shù)據(jù)的采集、查詢、集群、過濾等功能能夠方便的進行擴展,正是由于有此框架,使得 Nutch 的插件開發(fā)非常容易,第三方的插件也層出不窮,極大的增強了 Nutch 的功能和聲譽。
缺點:
Nutch的爬蟲定制能力比較弱
- heritrix3
https://github.com/internetarchive/heritrix3
GitHub stars = 1192
特點
- 能夠同時運行多個抓取任務,唯一的限制是要給并行運行的抓取任務分配內存.
- 基于Spring框架去管理XML配置.并且只用這一個XML配置就替換Hertrix1.X的order.xml和其他配置文件.
- 可以通過瀏覽器工具很方便易用的瀏覽和修改Spring Bean.
- 增強擴展了Spring框架.可以配置得很細致.具體見Sheets.
- 更安全的控制臺限制.通過HTTPS去訪問和操作控制臺.
- 增強了擴展性.以前的版本,如果有千萬級以上的種子都會先載入內存,如此有可能使得超過分配給Heritrix的內存導致內存溢出.Heririx3.0則解決了這個問題.允許這種大規(guī)模抓取.
- 可以靈活的修改一個正在運行的抓取任務.通過修改Bean和Action Directory兩種方式來修改.
- 引入了并行隊列.當抓取指定的站點以前只有一個隊列,如此會導致該隊列的過于龐大.和抓取緩慢.并行隊列的話,會將同一個站點的URL分成多個隊列去并行抓取.
- 增加了腳本控制臺,可以通過輸入各種各樣的腳本,如AppleScript,ECMAScript,Python,JS去控制和訪問Heritrix的基本組件運行情況(很有意思).

- Gecco
https://github.com/xtuhcy/gecco
GitHub stars = 1171
主要特征
- 簡單易用,使用jquery風格的選擇器抽取元素
- 支持爬取規(guī)則的動態(tài)配置和加載
- 支持頁面中的異步ajax請求
- 支持頁面中的javascript變量抽取
- 利用Redis實現(xiàn)分布式抓取,參考gecco-redis
- 支持結合Spring開發(fā)業(yè)務邏輯,參考gecco-spring
- 支持htmlunit擴展,參考gecco-htmlunit
- 支持插件擴展機制
- 支持下載時UserAgent隨機選取
- 支持下載代理服務器隨機選取

Ruby
Wombat
https://github.com/felipecsl/wombat
Github stars = 1083
Wombat is a simple ruby DSL to scrape webpages on top of the cool Mechanize and Nokogiri gems. It is aimed to be a more high level abstraction if you dont want to dig into the specifics of getting the page and parsing it into your own data structure, which can be a decent amount of work, depending on what you need.
最后,Python
- Scrapy
https://github.com/scrapy/scrapy
GitHub stars = 27682
- 內建的CSS選擇器和XPath表達式
- 基于IPython交互式shell,方便編寫爬蟲和debug
- 內建的文件導出和保存方法,格式多樣JSON、CSV、XML
- 健壯的編碼支持
- 擴展性強,可以使用signals和API(中間件、插件、管道)添加自定義功能
- 多種用于處理session、cookies、HTTP認證、user-agent、robots.txt、抓取深度限制的中間件和插件
- Scrapy內建Telnet console,可用于debug

- PySpider
https://github.com/binux/pyspider
GitHub star = 11418
特點
- Powerful WebUI with script editor, task monitor, project manager and result viewer
- MySQL, MongoDB, Redis, SQLite, Elasticsearch; PostgreSQL with SQLAlchemy as database backend
- RabbitMQ, Beanstalk, Redis and Kombu as message queue
- Task priority, retry, periodical, recrawl by age, etc...
- Distributed architecture, Crawl Javascript pages, Python 2.{6,7}, 3.{3,4,5,6} support, etc...

Scheduler
The Scheduler receives tasks from newtask_queue from processor. Decide whether the task is new or requires re-crawl. Sort tasks according to priority and feeding them to fetcher with traffic control (token bucket algorithm). Take care of periodic tasks, lost tasks and failed tasks and retry later.
Note that in current implement of scheduler, only one scheduler is allowed.
Scheduler
The Scheduler receives tasks from newtask_queue from processor. Decide whether the task is new or requires re-crawl. Sort tasks according to priority and feeding them to fetcher with traffic control (token bucket algorithm). Take care of periodic tasks, lost tasks and failed tasks and retry later.
Processor
The Processor is responsible for running the script written by users to parse and extract information. Your script is running in an unlimited environment. Although we have various tools(like PyQuery) for you to extract information and links, you can use anything you want to deal with the response. You may refer to Script Environment and API Reference to get more information about script.
Result Worker (optional)
Result worker receives results from Processor. Pyspider has a built-in result worker to save result to resultdb. Overwrite it to deal with result by your needs.
WebUI
WebUI is a web frontend for everything. It contains:
- script editor, debugger
- project manager
- task monitor
- result viewer, exporter
Maybe webui is the most attractive part of pyspider. With this powerful UI, you can debug your scripts step by step just as pyspider do. Starting or stop a project. Finding which project is going wrong and what request is failed and try it again with debugger.
- Newspaper
https://github.com/codelucas/newspaper
GitHub star = 6386
下面這個demo站,可以展示提取標題、正文、關鍵詞等信息。
http://newspaper-demo.herokuapp.com
Features
- Multi-threaded article download framework
- News url identification
- Text extraction from html
- Top image extraction from html
- All image extraction from html
- Keyword extraction from text
- Summary extraction from text
- Author extraction from text
- Google trending terms extraction
- Works in 10+ languages (English, Chinese, German, Arabic, …)
