简单好用的nodejs 爬虫框架分享

2020-06-17 05:52:50易采站长站整理

* crawler_handle: 与队列通信的对象,该对象包含以下方法
* .info : crawl-pet 的配置信息
* .uri : 当前请求的 uri 信息
* .addPage(url) : 向队列里添加一个待解析页面
* .addDown(url) : 向队列里添加一个待下载文件
* .save(content, ext) : 保存文本到本地,ext 设置保存文件的后缀名
* .over() : 结束当前队列,取出下一条队列数据
*/

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)){
// 这理添加了一个待解析页面
crawler_handle.addPage(href)
}
}
// 记得在解析结束后一定要执行
crawler_handle.over()
}

在最后会有一个分享,懂得的请往下看

第三步:查看爬取下来的数据

根据以下载到本地的文件,查找下载地址


$ crawl-pet -f ./test-crawl-pet/photos.foodshot.co/*.jpg


查找下载地址

查看等待队列


$ crawl-pet -l queue


查看等待队列

查看已下载的文件列表

$ crawl-pet -l down # 查看已下载列表中第 0 条后的5条数据 $ crawl-pet -l down,0,5 # –json 参数表示输出格式为 json $ crawl-pet -l down,0,5 –json


已下载的文件

查看已解析页面列表,参数与查看已下载的相同

$ crawl-pet -l page

基本功能就这些了,看一下它的帮助吧

该爬虫框架是开源的,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