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

wildspider

v1.2.23

Published

```typescript import { CrawCallbackParam, Spider, Schedule } from 'wildspider'

Downloads

96

Readme

开发

示例爬虫

import { CrawCallbackParam, Spider, Schedule } from 'wildspider'

export default class Example extends Spider {
    // 声明项目名称
    project = 'example'

    // 指定运行时间
    @Schedule.cron('0 */1 * * * *')
    start () {
        const startUrl = 'http://money.163.com/'
        // 使用dispatch 分配下一个任务,第一个参数为下一步要使用的方法
        this.dispatch(this.index, {
            url: startUrl
        })
    }

    // age表示url有效性,指定时间内url相同的链接不会重新采集
    // @Schedule.age({ minute: 2 })
    index ({ req, res }: CrawCallbackParam) {
        // 使用 cheerio 和使用(jQuery)一样获取内容
        const $ = res.doc()
        const latestNews = $('#ln_list1 li a')
        latestNews.each((index, a) => {
            const href = $(a).attr('href')
            this.dispatch(this.detail, {
                url: href
            })
        })
    }

    // age设置的很大意味着不会重新采集
    @Schedule.age({ day: 1000000 })
    @Schedule.returnResult('article')
    async detail ({ req, res }: CrawCallbackParam) {
        const $ = res.doc()
        const article: any = {}
        article.url = req.url
        article.title = $('#epContentLeft h1').text()
        const timeAndSrcNode = $('#epContentLeft .post_time_source').text()
        const timeMatch = timeAndSrcNode.match(/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}/)
        if (timeMatch && timeMatch.length > 0) {
            article.datetime = timeMatch[0]
        }
        const fromMatch = timeAndSrcNode.match(/(?:来源\:)\s*(.+)\s*/)
        if (fromMatch && fromMatch.length > 1) {
            article.from = fromMatch[1]
        } else {
            article.from = '163'
        }
        article.content = $('#endText').html()
        return article
    }
}

Spider方法

方法需要的参数参考代码

  • this.dispatch 分配下一个任务
  • this.save 保存结果
  • this.saveFile 保存文件,需传入内容
  • this.downLoadAndSaveFile 使用爬虫爬取文件并保存
  • this.sendMessage 给其他爬虫发消息(暂未使用)

装饰器说明:

装饰器出现在爬虫的方法上,改变其默认的行为配置

  • @Schedule.cron('0 */1 * * * *')
    爬虫需要定时执行的方法,
    只支持在start方法上
    2.0 将支持任何方法(未部署)

  • @Schedule.returnResult('article') 将方法的返回值作为结果保存,参数为 需要保存的 【队列】 支持生成器 yiled 返回多个结果

  • @Schedule.age()
    爬虫的有效期,有效期内相同的请求不会被重复爬取

  • @Schedule.faildDelay(number:second)
    爬取失败后,等等x秒后重试

  • @Schedule.reqInterval(number:second)
    每个任务之间,需要间隔 x秒

  • @Schedule.noWait()
    每个任务之间,不需要等待,尽快执行,相当于 reqInterval(0)

  • @Schedule.handleNon200(codes?:number[]) 接受非200返回,默认只有返回200才认为成功,否则认为请求失败,不会进入方法体

  • @Schedule.timeout(timeout:number) 请求的最大超时

  • @Schedule.ignoreSslError() 忽略ssl证书错误

  • @Schedule.priority(priority:number) 任务的优先级,支持0-32,任务优先级越高,越先爬取,默认为8