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


Posted in NodeJs onMarch 26, 2017

这个就是一篇介绍爬虫框架的文章,开头就不说什么剧情了。什么最近一个项目了,什么分享新知了,剧情是挺好,但介绍的很初级,根本就没有办法应用,不支持队列的爬虫,都是耍流氓。 所以我就先来举一个例子,看一下这个爬虫框架是多么简单并可用。

第一步:安装 Crawl-pet

nodejs 就不用多介绍吧,用 npm 安装 crawl-pet

$ npm install crawl-pet -g --production

运行,程序会引导你完成配置,首次运行,会在项目目录下生成 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

这里使用的测试网站 http://foodshot.co/ 是一个自由版权的,分享美食图片的网站,网站里的图片质量非常棒,这里用它只是为测试学习用,大家可以换其它网站测试

如果使用默认解析器的话,已经可以运行,看看效果:

$ crawl-pet -o ./test-crawl-pet

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

试试看

这是下载后的目录结构

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

本地目录结构

 第二步:写自己的解析器

现在我们来看一看如何写自己的解析器,有三种方法来生成我们自己的解析器

在新建项目时, 在 Set parser rule module 输入自己的解释器路径。修改 info.json 下的 parser 项这个最简单,直接在项目录下新建一个 parser.js 文件

使用 crawl-pet, 新建一个解析器模板

$ crawl-pet --create-parser ./test-crawl-pet/parser.js

打开 ./test-crawl-pet/parser.js 文件

// crawl-pet 支持使用 cheerio,来进行页面分析,如果你有这个需要
const cheerio = require("cheerio")

/*
 * header 函数是在请求发送前调用,可以配置请求的头信息,如果返回 false,则中断请求
 *
 * 参数:
 *  options:   详细设置请看 https://github.com/request/request
 *  crawler_handle: 与队列通信的对象,详情见下
 *
 * header 函数是可选的,可不写
 */
exports.header = function(options, crawler_handle) {  
}

/*
 * body 函数是在请求返回后调用,用来解析返回结果
 *
 * 参数:
 *  url:   请求的 url
 *  body:   请求返回结果, string 类型
 *  response:  请求的响应,详情请看: https://github.com/request/request
 *  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

简单好用的nodejs 爬虫框架分享
查找下载地址

查看等待队列

$ crawl-pet -l queue

简单好用的nodejs 爬虫框架分享
查看等待队列

查看已下载的文件列表

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

简单好用的nodejs 爬虫框架分享
已下载的文件

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

$ 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
 --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

最后分享一个配置

$ 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

本站下载地址:点击下载

NodeJs 相关文章推荐
Nodejs+express+html5 实现拖拽上传
Aug 08 NodeJs
轻松创建nodejs服务器(4):路由
Dec 18 NodeJs
轻松创建nodejs服务器(7):阻塞操作的实现
Dec 18 NodeJs
NodeJS和BootStrap分页效果的实现代码
Nov 07 NodeJs
nodejs进阶(6)—连接MySQL数据库示例
Jan 07 NodeJs
详解nodejs express下使用redis管理session
Apr 24 NodeJs
nodejs处理图片的中间件node-images详解
May 08 NodeJs
NodeJS自定义模块写法(详解)
Jun 27 NodeJs
nodejs简单访问及操作mysql数据库的方法示例
Mar 15 NodeJs
详解NodeJs开发微信公众号
May 25 NodeJs
nodejs中request库使用HTTPS代理的方法
Apr 30 NodeJs
nodejs环境使用Typeorm连接查询Oracle数据
Dec 05 NodeJs
nodejs开发——express路由与中间件
Mar 24 #NodeJs
详解NodeJS框架express的路径映射(路由)功能及控制
Mar 24 #NodeJs
NodeJS学习笔记之Module的简介
Mar 24 #NodeJs
详解nodejs中的process进程
Mar 19 #NodeJs
nodejs中使用HTTP分块响应和定时器示例代码
Mar 19 #NodeJs
nodejs中向HTTP响应传送进程的输出
Mar 19 #NodeJs
实例分析nodejs模块xml2js解析xml过程中遇到的坑
Mar 18 #NodeJs
You might like
thinkphp中html:list标签传递多个参数实例
2014/10/30 PHP
ThinkPHP查询语句与关联查询用法实例
2014/11/01 PHP
WIN8.1下搭建PHP5.6环境
2015/04/29 PHP
php判断访问IP的方法
2015/06/19 PHP
JavaScript 字符编码规则
2009/05/04 Javascript
JavaScript 对象链式操作测试代码
2010/04/25 Javascript
JavaScript函数模式详解
2014/11/07 Javascript
jQuery仿淘宝网产品品牌隐藏与显示效果
2015/09/01 Javascript
js自定义QQ菜单效果
2017/01/10 Javascript
原生js实现电商侧边导航效果
2017/01/19 Javascript
javascript 动态生成css代码的两种方法
2017/03/17 Javascript
jQuery复合事件结合toggle()方法的用法示例
2017/06/10 jQuery
Vue页面跳转动画效果的实现方法
2018/09/23 Javascript
微信小程序转发事件实现解析
2019/10/22 Javascript
JS通过识别id、value值对checkbox设置选中状态
2020/02/19 Javascript
[02:44]2014DOTA2 国际邀请赛中国区预选赛 大神红毯秀
2014/05/25 DOTA
[28:57]EG vs VGJ.T 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/16 DOTA
python中关于日期时间处理的问答集锦
2013/03/08 Python
python使用PIL模块实现给图片打水印的方法
2015/05/22 Python
python爬虫爬取网页表格数据
2018/03/07 Python
对python中数据集划分函数StratifiedShuffleSplit的使用详解
2018/12/11 Python
详解PyCharm安装MicroPython插件的教程
2019/06/24 Python
浅谈python已知元素,获取元素索引(numpy,pandas)
2019/11/26 Python
python初步实现word2vec操作
2020/06/09 Python
matplotlib绘制多子图共享鼠标光标的方法示例
2021/01/08 Python
西班牙高科技产品购物网站:MejorDeseo
2019/09/08 全球购物
JBL加拿大官方商店:扬声器、耳机等
2020/10/23 全球购物
求职信的最佳写作思路
2014/02/01 职场文书
思想品德自我评价
2014/02/04 职场文书
公司接待方案
2014/03/08 职场文书
党校学习心得体会范文
2014/09/09 职场文书
税务职业生涯规划书范文
2014/09/16 职场文书
《1942》观后感
2015/06/08 职场文书
学校运动会简讯
2015/07/20 职场文书
SpringBoot+Redis实现布隆过滤器的示例代码
2022/03/17 Java/Android
Python pyecharts案例超市4年数据可视化分析
2022/08/14 Python