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

webscrape

v3.0.0

Published

Scrape web pages. Uses and returns promises

Downloads

269

Readme

webscrape

npm downloads Build Status Dependency Status Coverage Status

webscrape is a convenience module that grabs stuff over HTTP and provides automatic parsing into JSON or a convenient jQuery selector to allow you to quickly manipulate the result. In addition, it also supports an endpoint to download files into your filesystem, either with the same name remotely, or to a specified folder / filename.

webscrape is Promise/A+ based via bluebird, and as a result also works well with ES7's async/await syntax.

usage

This module uses default exports. In ES6, you would do

import Scraper from 'webscrape';

const scraper = Scraper();

However in ES5, you would do

var webscrape = require('webscrape');
var scraper = webscrape.default();

api

webscrape uses a standard syntax for all invocations. Currently it supports the 2 basic HTTP methods, GET and POST, and are invoked via

  • scraper.get(url, options)
  • scraper.post(url, options)

In addition, there is also

  • scraper.download(url, options)

All invocations return A+ Promises. This works well with async/await in ES7.

  1. The first argument url is always the URL you want to scrape
  2. The second argument is an object that contains additional options regarding the HTTP request you want to make.
    • For .get, { headers, query } is supported
    • For .post, { headers, query, body } is supported.
    • For .download, { headers, query, filename, post } is supported.

e.g. scraper.get("http://www.google.com/search", { query: { q: 'pineapples' } });

parameters

  • headers should be a simple JavaScript object representing the HTTP headers you want to send.
  • query is also a JavaScript object of the query params you want to send.
  • body is a JavaScript object of the FORM params you want to POST (multipart is not currently supported)
  • filename is a string - the path of where you want the file you are downloading to be saved as, otherwise the original name is used.
  • post - this is a special parameter available for download only. It changes the method to POST and should be an object which contains the POST body.

response

Apart from the download API, the response will be a "result" object with one or more additional properties

  • It will always have a .body property representing the entirety of the response body, along with .headers
  • If the response is of type application/json, then there will be a .json property which is essentially the parsed JSON object.
  • If the response is of type text/html, there will be a .$ which, via the cheerio library, provides jQuery-like selector functionality.

examples (ES6/2015):

import Scraper from 'webscrape';

const scraper = Scraper();

async function main() {
    const result = await scraper.get('https://www.google.com');
    console.log(result.body); // dumps the raw HTML
    console.log(result.$('title').text()); // returns "Google"

    const query = {
        'address': '1600 Amphitheatre Parkway, Mountain View, CA',
        'sensor': 'false'
    };
    const result2 = await scraper.get('http://maps.googleapis.com/maps/api/geocode/json', { query })
    console.log(result2.json); // gets JSON information about the Google's Headquarters
}

async function execute() {
	try {
		await main();
	} catch (e) {
		console.error(e.stack || e);
	}
}

execute();

Please refer to test.js for more ES6 examples.

P.S. This replaces the older qscraper library with a more efficient API. This module is currently for Node only, it is not a universal or client-side module.