npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

ld-spider

v1.0.3

Published

`ld-spider`是一个基于http/https协议的接口访问包,依赖`axios`,主要功能包括: - 数据爬取队列控制 - 接口并发控制 - 接口爬取时间范围随机

Downloads

2

Readme

概述

ld-spider是一个基于http/https协议的接口访问包,依赖axios,主要功能包括:

  • 数据爬取队列控制
  • 接口并发控制
  • 接口爬取时间范围随机

用法

// 主域名,爬取限制是按主域来做限制
const domain = 'www.baidu.com'
// 是否开启调试,开启后输出日志
const debug = true
// 爬取规则配置
const rule = {
  // random=随机范围,单位ms; fixed=固定时间,单位ms
  type: 'random', 
  // 当type=random时,该配置有效,表示随机的时间范围
  range: {
    min: 100, 
    max: 200
  },
  // 当type=fixed时,该配置有效,表示固定的时间间隔
  fixedTime: 200,
  // 并发数,表示每次爬取时的并发数据量
  concurrent: 10, 
}


// 初始化spider
const spider = Spider(domain, rule, debug);


// 设置持久化回调
spider.setPersist(async function(opt, res) {
  // TODO 这里可以处理res结果
})


// 任务格式
const task = {
  // 必填,请求方法
  method: 'get|post|put|delete',
  // 必填,请求路径,支持完整地址的写法,如果只写路径会自动跟domain拼接起来
  url: '/api/***|http://***/api/***'
  // 非必填,请求query
  query: {},
  // 非必填,请求data
  data: {},
  // 非必填,请求headers
  headers: {}
}


// 添加爬取任务,有三种添加方法
const pre = false // 用来标识加入的任务是否提前,如果等于true,下一次任务为当前插入任务
// 方法一, 第一个参数为function
spider.addTask(function() {
  // TODO 经过处理后返回任务或任务列表
  return task || taskList
}, pre)
// 方法二,第一个参数为数组
const taskArr = [
  task1,
  task2,
  ...
]
spider.addTask(taskArr, pre)
// 方法三,第一个参数为对象
spider.addTask(task, pre)


// 开始爬取,该方法会一直爬取直到任务队列里面没有任务,期间可以通过addTask方法添加任务
spider.start()

api

setDebug(state = false)

设置是否开启调试模式

// 关闭调试模式
spider.setDebug(false)
// 开启调试模式
spider.setDebug(true)

setRule(rule = {})

设置爬取规则,设置完成后立即生效,作用到下个任务

const rule = {
  // random=随机范围,单位ms; fixed=固定时间,单位ms
  type: 'random', 
  // 当type=random时,该配置有效,表示随机的时间范围
  range: {
    min: 100, 
    max: 200
  },
  // 当type=fixed时,该配置有效,表示固定的时间间隔
  fixedTime: 200,
  // 并发数,表示每次爬取时的并发数据量
  concurrent: 10, 
}
spider.setRule(rule)

setPersist(fn)

设置持久化回调,如果没有配置,执行任务爬取后只会无脑爬取,没有实际作用,如果有配置回调方法,接口在爬取结束后会自动调用fn方法,并传入[opts,res]作为fn的入参

// opt参数为任务请求参数,res为请求结束返回内容
spider.setPersist(async function(opt) {
  // TODO 这里可以做自己的数据持久化操作
})