1.0.3 • Published 4 years ago

linuocc-spider v1.0.3

Weekly downloads
2
License
MIT
Repository
github
Last release
4 years ago

基于node的爬虫封装

1.安装

npm install linuocc-spider

2.使用

//导入linuocc-spider
const Spider = require("linuocc-spider");


//实例化spider
const spider = new Spider(["http://baidu.com"], {
  linkPoolMaxNum: 500,
  delayTime: 500,
  timeout: 3000,
  encoding: "utf8",
  fileTypesIgnored: ["js", "css", "json"],
  domainIgnored: ["github.com"],
  onlySite: true
});

//调用开始爬取方法,方法需要传入一个回调函数,回调函数有两个参数,一个是返回的数据,一个是触发下一次爬取的方法,数据处理完之后一定要调用next()方法,否则不会继续爬取
spider.start((res, next) => {
  console.log(res.url);
  
  next();
  
});

3.配置项

配置名说明类型默认值
linkPoolMaxNum链接池(存储待爬取链接的容器)最大容量,number500
delayTime两次爬取之间的间隔(单位毫秒)number0
timeout爬取超时时间number5000
encoding响应数据的编码方式stringutf8
fileTypesIgnored忽略爬取的文件类型array"js", "css", "json"
domainIgnored忽略爬取的域名,包括二级域名,默认为空array
onlySite是否只爬取本站点,默认为否,爬取其他站点booleanfalse

4.返回数据

spider.start(callback)

callback(result,next):回调函数
result:返回的结果,为一个对象
属性名说明类型
url当前爬取的页面urlstring
data当前爬取页面的全部html数据string

​ result示例

{
	url:"http://www.baidu.com/a.php?a=b",
    data:'<!DOCTYPE html><html lang="en"><head><meta charset="UTF-8"><title>Title</title></head><body></body></html>'
}
next:触发进行下一次爬取的方法

​ next示例

spider.start((res, next) => {
  console.log(res.url);
  /*
  这里进行业务处理
  */
  next();//处理完之后调用next方法
});

更新日志

1.0.3

过滤域名之后的哈希值

1.0.1

新增是否只爬取目标网站页面的配置项

好像可以做点事了( ^ ▽ ^

1.0.0

完成简单的功能,除了能爬,什么都做不了┓( ´∀` )┏

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago