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

scavenger

v3.3.0

Published

Command line tool / Node.js package using Nightmarejs to scrape/take screenshots of dynamic and static webpages.

Downloads

7

Readme

Scavenger

Command line tool / Node.js package for scraping/taking screenshots of dynamic and static webpages using Nightmare.

Features

  1. Can extract data from html and convert it to JSON (only in programmatic use).
  2. Supports dynamic (Angularjs, etc) and static web pages.
  3. Can be piped to other programs.
  4. Can be used from command line or programmatically
  5. Runs on any linux based os. (Probably on windows and mac too, but it hasn't been tested yet)

Install

As a global package:

$ npm install -g scavenger

As a local package:

$ npm install scavenger

Programmatic usage

const scavenger = require('scavenger');

Minimalistic usage:

scavenger.scrape("https://reddit.com")
.then((html) => {})

API

.scrape(url, options, mapFn)

url can be either a String or an Array, in which case scavenger will scrape every given url in sequence.

The result can be a String or an Array depending on url and mapFn.

mapFn is a function which is executed for every url and takes as argument the html of the scraped page. Can be passed as second argument if no options are passed. See .extract and .createExtractor for more info.

scavenger.scrape(url)
.then((html) => {
    console.log(html);
    // '<body>....</body>'
});

// Or
scavenger.scrape(url, {    
    selector: '#id', // ID of a DOMElement to wait before scraping
    minify: false, // If true, minify the html
    driverFn: function(){}, // A function that is evaluated in Nightmarejs context to interact with the page,
    useragent: 'Scavenger - https://www.npmjs.com/package/scavenger', // By default,
    nightmareOptions: {} // This options go directly to the Nightmarejs constructor
})
.then((html) => {});


// Multiple urls with mapFn (get length of html for each scraped page)
scavenger.scrape(urls, {/*options*/}, html => html.length)
.then((htmlLengths) => {
    console.log(htmlLengths);
    // [10040, 22351, ...]
});

.screenshot(url, options)

Returns an object of buffers of the screenshot.

scavenger.screenshot(url)
.then((buffers) => {
    console.log(buffers);
    // {
    //     "full": <Buffer>
    // }
});

// Or

scavenger.screenshot(url, {    
    selector: '#id', // ID of a DOMElement to wait before scraping
    format: 'png', // Default: png. Available: jpeg, png.
    crop: [{
        width: 1280,
        height: 680
    }, ...],
    width: 1280, // Viewport width in pixels. By default it adapts to the page width. Height is always 100% of the page.
    useragent: 'Scavenger - https://www.npmjs.com/package/scavenger', // By default
    nightmareOptions: {} // This options go directly to the Nightmarejs constructor
})
.then((buffers) => {
    console.log(buffers);
    // {
    //     "full": <Buffer>,
    //     "1280X680": <Buffer>
    // }
});

.ss(url, options, mapFn)

Combines .scrape and .screenshot. If mapFn is passed, it will be executed on html only.

scavenger.ss(url, {    
    selector: '#id',
    minify: false,
    driverFn: function(){},
    nightmareOptions: {},
    format: 'png',
    crop: [{
        width: 1280,
        height: 680
    }, ...],
    width: 1280
})
.then((result) => {
    console.log(result);
    // {
    //    html: '',
    //    buffers: {
    //        "full": <Buffer>,
    //        "1280X680": <Buffer>
    //    }
    // }
});

.extract(html, options)

See also the examples.

Extracts text from given html and returns it in json format. Supports tables or any element.

Generic HTML elements:

const authors = scavenger.extract(html, {
    scope: '.class', // Any css selector
    fields: { // Fields are found within the scope element if given
        author: 'h3.author', // Any css selector
        url: {
            selector: 'a.link',
            attribute: 'href' // Gets the href attribute value for the element found at selector
        },
        any: '',
        ...
    },
    groupBy: 'author' // a field name to group results by
});

// Or passing just the fields
scavenger.extract(html, {    
    author: 'h3.author',
    url: {
        selector: 'a.link',
        attribute: 'href'
    },
    any: '',
    ...    
});

.createExtractor(options, fn)

See also the examples.

Helper method. Returns an extract function which can be passed to .scrape as mapFn.

If no fn is passed, .extract will be used by default.

const extract = scavenger.createExtractor({
    scope: 'section',
    fields: {
        title: 'h1.fly-title',
        headline: 'article h2.headline',
        rubric: 'article p.rubric'
    }
});

return scavenger.scrape('http://www.economist.com', extract);

.paginateUrl(options, fn)

Helper method. Returns an array of urls with the correct query for pagination.

const urls = scavenger.paginateUrl({
    baseUrl: 'https://www.google.com/search?',
    params: {
        q: 'scavenger scraper',
        start: 0
    },
    paginationParam: 'start',
    limit: 30,
    step: 10
});

// [
//     'https://www.google.com/search?q=scavenger%20scraper&start=0',    
//     'https://www.google.com/search?q=scavenger%20scraper&start=10'
// ]

scavenger.scrape(urls);

Command line usage

Help

$ scavenger -h

Screenshot

Save image to a png file:

$ scavenger screenshot -u https://reddit.com
$ # Creates a file called https_reddit_com.png

Pipe image to ImageMagick display and show it:

$ scavenger screenshot -u https://reddit.com | display

Scrape

Pipe html to less:

$ scavenger scrape -u https://reddit.com | less

Save html to a file:

$ scavenger screenshot -u https://reddit.com > reddit.html

Or

$ scavenger screenshot -u https://reddit.com
$ # Creates a file called https_reddit_com.html

Scrape + Screenshot

$ scavenger ss -u https://reddit.com

License

MIT