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

csscrape

v2.0.1

Published

A simple, lightweight, promise-based web scraper.

Downloads

15

Readme

csscrape

A simple, lightweight, promise-based web scraper for Node.js.

npm version Build Status

Install

$ npm install csscrape

Basic Usage

Example - Try it out in your browser

Scraping the most dependend-upon packages from NPM's main page:

const { scrape } = require('csscrape');

await scrape('www.npmjs.com/browse/depended')
    .select('main h3')
    .done();

/*
Results (as of 5/25/2020):
[
  'lodash',      'react',      'chalk',
  'request',     'commander',  'moment',
  'express',     'react-dom',  'prop-types',
  'tslib',       'axios',      'debug',
  'fs-extra',    'async',      'bluebird',
  'vue',         'uuid',       'classnames',
  'core-js',     'underscore', 'inquirer',
  'yargs',       'webpack',    'rxjs',
  'mkdirp',      'glob',       'body-parser',
  'dotenv',      'colors',     'typescript',
  'jquery',      'minimist',   'babel-runtime',
  '@types/node', 'aws-sdk',    '@babel/runtime'
]
*/

Use simple JSON & CSS to describe the data you want

Example - Try it out in your browser

Same as above, but only get the first 2 packages and their details:

const { scrape } = require('csscrape');

await scrape('www.npmjs.com/browse/depended')
    .filter('main section:nth-child(-n+2)')
    .select({
        name: 'h3',
        info: {
            desc: 'p',
            author: 'h4 + div a',
            info: 'h4 + div span',
        }
    })
    .done();

/*
Results (as of 5/25/2020):
[
  {
    name: 'lodash',
    info: {
      desc: 'Lodash modular utilities.',
      author: 'jdalton',
      info: 'published 4.17.15 • 10 months ago'
    }
  },
  {
    name: 'react',
    info: {
      desc: 'React is a JavaScript library for building user interfaces.',
      author: 'acdlite',
      info: 'published 16.13.1 • 2 months ago'
    }
  }
]
*/

Follow links to scrape related data

Example - Try it out in your browser

Same as above, but follow each package's link to grab number of dependencies from its details page:

const { scrape } = require('csscrape');

await scrape('www.npmjs.com/browse/depended')
    .filter('main section:nth-child(-n+2)')
    .select({
        name: 'h3',
        info: {
            desc: 'p',
            author: 'h4 + div a',
            info: 'h4 + div span',
        }
    })
    .follow('a[href^="/package"]')
    .select({
        dependencies: 'main > div > ul li:nth-child(3) a span span',
        dependents: 'main > div > ul li:nth-child(4) a span span',
    })
    .done();

/*
Results (as of 5/25/2020):
[
  {
    name: 'lodash',
    info: {
      desc: 'Lodash modular utilities.',
      author: 'jdalton',
      info: 'published 4.17.15 • 10 months ago'
    },
    dependencies: '0',
    dependents: '115,408'
  },
  {
    name: 'react',
    info: {
      desc: 'React is a JavaScript library for building user interfaces.',
      author: 'acdlite',
      info: 'published 16.13.1 • 2 months ago'
    },
    dependencies: '3',
    dependents: '56,464'
  }
]
*/

Simple API

You can scrape for data using the five following methods:

/**
 * Get the initial url to start the scrape
 * @param {string} url - the url of the page to scrape
 */
scrape(url);

/**
 * Filter down the current results to create a new data context
 * @param {string} selector - a css selector string
 */
filter(selector);

/**
 * Select data from the current data context
 * @param {string | string[] | {}} selector - a css selector string/object
 * Note: To select an attribute value instead of text use string array:
 * Select div text           : .select('div')
 * Select div title attribute: .select(['div', 'title'])
 */
select(selector);

/**
 * Find a link in the current data context to follow to continue scraping
 * @param {string} selector - a css selector string
 */
follow(selector);

/**
 * Marks the current scrape as finished and returns a Promise of the results
 */
done();

Command Line Interface

csscrape also provides a CLI

Usage: csscrape [options] <url>

Options:
  -V, --version                output the version number
  -f, --filter <selector>      Filter to specific data in the results
  -s, --select <selector>      Data selector (string or json string)
  -l, --followlink <selector>  Select a link from the data to follow
  -v, --verbose                Set logging to verbose
  -h, --help                   display help for command

Install

$ npm install csscrape -g
Example

Same as first example, but using the CLI

csscrape www.npmjs.com/browse/depended -s 'main h3'

# Results (as of 5/25/2020):
# [
#   'lodash',      'react',      'chalk',
#   'request',     'commander',  'moment',
#   'express',     'react-dom',  'prop-types',
#   'tslib',       'axios',      'debug',
#   'fs-extra',    'async',      'bluebird',
#   'vue',         'uuid',       'classnames',
#   'core-js',     'underscore', 'inquirer',
#   'yargs',       'webpack',    'rxjs',
#   'mkdirp',      'glob',       'body-parser',
#   'dotenv',      'colors',     'typescript',
#   'jquery',      'minimist',   'babel-runtime',
#   '@types/node', 'aws-sdk',    '@babel/runtime'
# ]