簡(jiǎn)單好用的nodejs 爬蟲(chóng)框架分享
這個(gè)就是一篇介紹爬蟲(chóng)框架的文章,開(kāi)頭就不說(shuō)什么劇情了。什么最近一個(gè)項(xiàng)目了,什么分享新知了,劇情是挺好,但介紹的很初級(jí),根本就沒(méi)有辦法應(yīng)用,不支持隊(duì)列的爬蟲(chóng)。 所以我就先來(lái)舉一個(gè)例子,看一下這個(gè)爬蟲(chóng)框架是多么簡(jiǎn)單并可用。
第一步:安裝 Crawl-pet
nodejs 就不用多介紹吧,用 npm 安裝 crawl-pet
$ npm install crawl-pet -g --production
運(yùn)行,程序會(huì)引導(dǎo)你完成配置,首次運(yùn)行,會(huì)在項(xiàng)目目錄下生成 info.json 文件
$ crawl-pet > Set project dir: ./test-crawl-pet > Create crawl-pet in ./test-crawl-pet [y/n]: y > Set target url: http://foodshot.co/ > Set save rule [url/simple/group]: url > Set file type limit: > The limit: not limit > Set parser rule module: > The module: use default crawl-pet.parser
這里使用的測(cè)試網(wǎng)站 http://foodshot.co/ 是一個(gè)自由版權(quán)的,分享美食圖片的網(wǎng)站,網(wǎng)站里的圖片質(zhì)量非常棒,這里用它只是為測(cè)試學(xué)習(xí)用,大家可以換其它網(wǎng)站測(cè)試
如果使用默認(rèn)解析器的話(huà),已經(jīng)可以運(yùn)行,看看效果:
$ crawl-pet -o ./test-crawl-pet

試試看
這是下載后的目錄結(jié)構(gòu)

本地目錄結(jié)構(gòu)
第二步:寫(xiě)自己的解析器
現(xiàn)在我們來(lái)看一看如何寫(xiě)自己的解析器,有三種方法來(lái)生成我們自己的解析器
在新建項(xiàng)目時(shí), 在 Set parser rule module 輸入自己的解釋器路徑。修改 info.json 下的 parser 項(xiàng)這個(gè)最簡(jiǎn)單,直接在項(xiàng)目錄下新建一個(gè) parser.js 文件
使用 crawl-pet, 新建一個(gè)解析器模板
$ crawl-pet --create-parser ./test-crawl-pet/parser.js
打開(kāi) ./test-crawl-pet/parser.js 文件
// crawl-pet 支持使用 cheerio,來(lái)進(jìn)行頁(yè)面分析,如果你有這個(gè)需要
const cheerio = require("cheerio")
/*
* header 函數(shù)是在請(qǐng)求發(fā)送前調(diào)用,可以配置請(qǐng)求的頭信息,如果返回 false,則中斷請(qǐng)求
*
* 參數(shù):
* options: 詳細(xì)設(shè)置請(qǐng)看 https://github.com/request/request
* crawler_handle: 與隊(duì)列通信的對(duì)象,詳情見(jiàn)下
*
* header 函數(shù)是可選的,可不寫(xiě)
*/
exports.header = function(options, crawler_handle) {
}
/*
* body 函數(shù)是在請(qǐng)求返回后調(diào)用,用來(lái)解析返回結(jié)果
*
* 參數(shù):
* url: 請(qǐng)求的 url
* body: 請(qǐng)求返回結(jié)果, string 類(lèi)型
* response: 請(qǐng)求的響應(yīng),詳情請(qǐng)看: https://github.com/request/request
* crawler_handle: 與隊(duì)列通信的對(duì)象,該對(duì)象包含以下方法
* .info : crawl-pet 的配置信息
* .uri : 當(dāng)前請(qǐng)求的 uri 信息
* .addPage(url) : 向隊(duì)列里添加一個(gè)待解析頁(yè)面
* .addDown(url) : 向隊(duì)列里添加一個(gè)待下載文件
* .save(content, ext) : 保存文本到本地,ext 設(shè)置保存文件的后綴名
* .over() : 結(jié)束當(dāng)前隊(duì)列,取出下一條隊(duì)列數(shù)據(jù)
*/
exports.body = function(url, body, response, crawler_handle) {
const re = /\b(href|src)\s*=\s*["']([^'"#]+)/ig
var m = null
while (m = re.exec(body)){
let href = m[2]
if (/\.(png|gif|jpg|jpeg|mp4)\b/i.test(href)) {
// 這理添加了一條下載
crawler_handle.addDown(href)
}else if(!/\.(css|js|json|xml|svg)/.test(href)){
// 這理添加了一個(gè)待解析頁(yè)面
crawler_handle.addPage(href)
}
}
// 記得在解析結(jié)束后一定要執(zhí)行
crawler_handle.over()
}在最后會(huì)有一個(gè)分享,懂得的請(qǐng)往下看
第三步:查看爬取下來(lái)的數(shù)據(jù)
根據(jù)以下載到本地的文件,查找下載地址
$ crawl-pet -f ./test-crawl-pet/photos.foodshot.co/*.jpg

查找下載地址
查看等待隊(duì)列
$ crawl-pet -l queue

查看等待隊(duì)列
查看已下載的文件列表

已下載的文件
查看已解析頁(yè)面列表,參數(shù)與查看已下載的相同
基本功能就這些了,看一下它的幫助吧
該爬蟲(chóng)框架是開(kāi)源的,GIthub 地址在這里:https://github.com/wl879/Crawl-pet
$ crawl-pet --help
Crawl-pet options help:
-u, --url string Destination address
-o, --outdir string Save the directory, Default use pwd
-r, --restart Reload all page
--clear Clear queue
--save string Save file rules following options
= url: Save the path consistent with url
= simple: Save file in the project path
= group: Save 500 files in one folder
--types array Limit download file type
--limit number=5 Concurrency limit
--sleep number=200 Concurrent interval
--timeout number=180000 Queue timeout
--proxy string Set up proxy
--parser string Set crawl rule, it's a js file path!
The default load the parser.js file in the project path
--maxsize number Limit the maximum size of the download file
--minwidth number Limit the minimum width of the download file
--minheight number Limit the minimum height of the download file
-i, --info View the configuration file
-l, --list array View the queue data
e.g. [page/down/queue],0,-1
-f, --find array Find the download URL of the local file
--json Print result to json format
-v, --version View version
-h, --help View help最后分享一個(gè)配置
$ crawl-pet -u https://www.reddit.com/r/funny/ -o reddit --save group
info.json
{
"url": "https://www.reddit.com/r/funny/",
"outdir": ".",
"save": "group",
"types": "",
"limit": "5",
"parser": "my_parser.js",
"sleep": "200",
"timeout": "180000",
"proxy": "",
"maxsize": 0,
"minwidth": 0,
"minheight": 0,
"cookie": "over18=1"
}my_parser.js
exports.body = function(url, body, response, crawler_handle) {
const re = /\b(data-url|href|src)\s*=\s*["']([^'"#]+)/ig
var m = null
while (m = re.exec(body)){
let href = m[2]
if (/thumb|user|icon|\.(css|json|js|xml|svg)\b/i.test(href)) {
continue
}
if (/\.(png|gif|jpg|jpeg|mp4)\b/i.test(href)) {
crawler_handle.addDown(href)
continue
}
if(/reddit\.com\/r\//i.test(href)){
crawler_handle.addPage(href)
}
}
crawler_handle.over()
}如果你是了解 reddit 的,那就這樣了。
GIthub 地址在這里:https://github.com/wl879/Crawl-pet
本站下載地址:點(diǎn)擊下載
相關(guān)文章
nodeJs爬蟲(chóng)獲取數(shù)據(jù)簡(jiǎn)單實(shí)現(xiàn)代碼
這篇文章主要為大家詳細(xì)介紹了nodeJs爬蟲(chóng)獲取數(shù)據(jù)簡(jiǎn)單實(shí)現(xiàn)代碼,感興趣的小伙伴們可以參考一下2016-03-03
通過(guò)nodejs 服務(wù)器讀取HTML文件渲染到頁(yè)面的方法
今天小編就為大家分享一篇通過(guò)nodejs 服務(wù)器讀取HTML文件渲染到頁(yè)面的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
Node.js和MongoDB實(shí)現(xiàn)簡(jiǎn)單日志分析系統(tǒng)
這篇文章主要介紹了Node.js和MongoDB實(shí)現(xiàn)簡(jiǎn)單日志分析系統(tǒng),本文給出了服務(wù)器端、客戶(hù)端、圖表生成、Shell自動(dòng)執(zhí)行等功能的實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
Node定時(shí)備份MySQL的實(shí)現(xiàn)
本文主要介紹了Node定時(shí)備份MySQL的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04
剖析Node.js異步編程中的回調(diào)與代碼設(shè)計(jì)模式
這篇文章主要介紹了Node.js異步編程中的回調(diào)與代碼設(shè)計(jì)模式,雖然大多數(shù)場(chǎng)合回調(diào)編寫(xiě)時(shí)的長(zhǎng)串括號(hào)不怎么好看,但Node的異步性能確實(shí)很好,需要的朋友可以參考下2016-02-02
使用node搭建自動(dòng)發(fā)圖文微博機(jī)器人的方法
這篇文章主要介紹了使用node搭建自動(dòng)發(fā)圖文微博機(jī)器人的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03
Node.js開(kāi)發(fā)之套接字(socket)編程入門(mén)示例
這篇文章主要介紹了Node.js開(kāi)發(fā)之套接字(socket)編程,結(jié)合簡(jiǎn)單實(shí)例形式分析了node.js套接字socket客戶(hù)端與服務(wù)器端相關(guān)實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下2019-11-11
基于Node.js實(shí)現(xiàn)一鍵生成個(gè)性化二維碼
這篇文章主要為大家詳細(xì)介紹了如何使用Node.js、Jimp和QRCode庫(kù),結(jié)合一個(gè)簡(jiǎn)單的腳本,通過(guò)命令行命令來(lái)快速給二維碼加上指定的背景,打造更有個(gè)性化的二維碼,感興趣的可以了解下2024-03-03
Node.js 多進(jìn)程處理CPU密集任務(wù)的實(shí)現(xiàn)
這篇文章主要介紹了Node.js 多進(jìn)程處理CPU密集任務(wù)的實(shí)現(xiàn),小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2019-05-05

