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

swallows

v1.1.1

Published

- [安装](#安装) - [使用](#使用) - [配置选项](#配置选项) - [demo](#demo)

Downloads

5

Readme

swallows 网页爬取工具

目录

安装

yarn add swallows --global

使用

  • init
swallows init

初始化并生成配置文件config.jsonconfig.json具体配置见配置选项

  • crawl
swallows crawl

默认读取当前运行目录下的config.json作为配置,并运行爬虫程序。

或者可以自定义配置文件路径,以当前运行目录为参照的相对路径

swallows crawl -src ../config.json
  • importUrls

从 json 文件导入 url 并写入到config.json

注意被导入的 json 文件内容是数组,并且数组内元素是一个包含 url 属性的对象。如:[{"name":"a","url":"https://www.baidu.com"}]

配置选项

  • name<string>

名称,爬虫程序完成后会生成以该名称命名的 json 文件存储结果

  • url<string | array>

爬取网站地址,可以是字符串,表示爬取网站的首个地址,也可以是字符串数组,会按顺序爬取网站

  • actions<array>

需要在网页上执行的动作,会按照数组的先后顺序执行。配置动作:

{
  "type": "click",
  "selector": "#button"
}

type表示动作类型,selector表示执行动作的元素选择器

  • pagination<object>

分页选项,包括页数count,以及触发下一页的选择器nextpageSelector

  • targetSelector<string>

需要采集数据的目标容器选择器,最好包裹住所有需要采集的内容

  • properties<array>

需要采集的数据字段及内容,具体配置:

{
  "name": "name",
  "selector": "div.name",
  "source": "innerText"
}

name:字段名,用于存储数据

selector:目标选择器,从targetSelector开始查找

source:采集内容的来源,可以理解为元素的属性。

demo

以爬取[imdb]top250 影片信息为例:

  1. 初始化
swallows init

按照命令行提示输入名称"imdb250",然后可以看到生成 imdb250 文件夹,里面包含config.json

  1. 配置选项

打开配置文件,并写入配置,如:

{
  "name": "imdb250",
  "url": "https://www.imdb.cn/imdb250/",
  "actions": [
    {
      "type": "",
      "selector": ""
    }
  ],
  "pagination": {
    "count": 0,
    "nextpageSelector": ""
  },
  "targetSelector": "div.ranking_lister > table > tbody > tr",
  "properties": [
    {
      "name": "name",
      "selector": "td.rl_name>a",
      "source": "title"
    },
    {
      "name": "url",
      "selector": "td.rl_name>a",
      "source": "href"
    },
    {
      "name": "rate",
      "selector": "td.rl_grade_IMDB>span",
      "source": "innerText"
    }
  ]
}
  1. 启动程序
swallows crawl

完成后会生成imdb250.json