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

@skypilot/scraper

v1.0.0-alpha.23

Published

Node-based scriptable web scraper

Downloads

12

Readme

@skypilot/scraper

npm latest downloads license: ISC

Node-base scriptable web scraper

How to use

  1. Create a database adapter
const dbFilePath = 'tmp/demo.json';
const database = new LowDb(dbFilePath);
  1. Create a scraper that uses the database
import { PlaywrightScraper } from './src/PlaywrightScraper';
const scraper = new PlaywrightScraper({ database });
  1. Use ScriptBuilder to build a script:
import { ScriptBuilder } from './src/ScriptBuilder';
const builder = new ScriptBuilder()
  .goTo('https://www.iana.org/domains/reserved') // start at a page
  .runOnAll({ // Runs the nested `commands` on each element that matches `query`
    query: 'table#arpa-table > tbody > tr > td > span.domain.label',
    commands: new ScriptBuilder()
      .follow('a') // follow the href in the first `a` tag
      .query({ // gather this data for each iteration of the elements matching the `runOnAll` query
        title: 'head > title',
        sponsor: '//h2[contains(text(), "Sponsoring Organisation")]/following-sibling::b',
        adminContact: '//h2[contains(text(), "Administrative Contact")]/following-sibling::b',
        techContact: '//h2[contains(text(), "Technical Contact")]/following-sibling::b',
      })
      .write() // writes to the database
  });
  1. Pass the script into the scraper's run method:
const result = scraper.run(builder);

Query

There are two ways to write a query:

1. A Query or ShorthandQuery object

A Query object is the standard way to write a selector:

interface Query {
  selector: string; // a CSS or XPath selector
  attributeName?: string; // if specified, select this attribute's value; otherwise, select the element's text content
  scope?: 'one' | 'all'; // default = 'one'; when used with `runOnAll`, `scope: 'all'` is automatically set
  limit?: Integer; // limits the selection to `limit` elements
  nthOfType?: Integer; // select the `nth` element matching the selector
}

A ShorthandQuery is the same as Query object, but uses a shorthand syntax for some of the keys:

interface ShorthandQuery {
  sel: string;
  attr?: string;
  scope?: 'one' | 'all';
  limit?: Integer;
  nth?: Integer;
}

See CSS and XPath selectors. Support for text selectors will be added soon.

A query matches the first element matching the selector, with two exceptions:

  • When used with runOnAll or when scope: 'all', the selector selects all matching elements up to the limit (if any)
  • When nthOfType is set, the selector selects the nth matching element

2. A string query

When a string value is used as the query, that value is treated as the selector param.

E.g., if the argument is 'h2', it is understood to mean { selector: 'h2' }.