# page-scraper

> Web page scraper with a jQuery-like syntax for Node.

Latest version **2.0.5** (published 2018-12-07) · MIT license · 0 weekly downloads

## Install

```sh
npm install page-scraper
pnpm add page-scraper
yarn add page-scraper
bun add page-scraper
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2018-12-07 |
| First published | 2016-03-02 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=8.0.0 |
| Dependencies | 2 |
| Unpacked size | 6.1 KB |
| Known vulnerabilities | 0 (+1 in 1 direct dependencies) |
| Install scripts | no |
| GitHub stars | 1 |
| Author | Risan Bagja Pradana |
| Maintainers | risan |
| Keywords | crawler, scraper, web-scraper |

## Links

- npm: https://www.npmjs.com/package/page-scraper
- Repository: https://github.com/risan/page-scraper
- Issues: https://github.com/risan/page-scraper/issues
- npm.io page: https://npm.io/package/page-scraper

## Dependencies (2)

- [got](https://npm.io/package/got.md) 9.x
- [cheerio](https://npm.io/package/cheerio.md) ^1.0.0-rc.2

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 2.0.5 (latest) — 2018-12-07
- 2.0.4 — 2018-12-07
- 2.0.3 — 2018-12-02
- 2.0.2 — 2018-11-11
- 2.0.1 — 2018-11-07
- 1.0.1 — 2016-03-02
- 1.0.0 — 2016-03-02

## README

# Page Scraper

[![Build Status](https://badgen.net/travis/risan/page-scraper)](https://travis-ci.org/risan/page-scraper)
[![Test Covarage](https://badgen.net/codecov/c/github/risan/page-scraper)](https://codecov.io/gh/risan/page-scraper)
[![Greenkeeper](https://badges.greenkeeper.io/risan/page-scraper.svg)](https://greenkeeper.io)
[![Latest Version](https://badgen.net/npm/v/page-scraper)](https://www.npmjs.com/package/page-scraper)

Web page scraper with a jQuery-like syntax for Node. Powered by [got](https://github.com/sindresorhus/got) and [cheerio](https://cheerio.js.org).

## Installation

```bash
$ npm install page-scraper
```

## Usage

```js
const scrape = require('page-scraper');

(async () => {
  const $ = await scrape('https://example.com');

  // Extract the page with jQuery like syntax.
  console.log({
    title: $('title').text(),
    heading: $('h1').text(),
    paragraphs: $('p').map((index, el) => $(el).text()).get(),
    link: $('p > a').attr('href')
  });
})();
```

Check the [cheerio documentation](https://cheerio.js.org/) for a complete guide on how to scrape the page using jQuery like syntax.

## Recipes

### Handling Error

```js
const scrape = require('page-scraper');

(async () => {
  try {
    const $ = await scrape('https://httpbin.org/status/400');
  } catch(error) {
    // The error message.
    console.error(error.message);

    if (error.hasOwnProperty('response')) {
      // The HTTP status code.
      console.error(error.response.statusCode);
    }

    if (error.hasOwnProperty('$')) {
      // The HTML document.
      console.error(error.$.html());
    }
  }
})();
```

Note that if the page is not an HTML document, it will throw an error too.

```js
const scrape = require('./src');

(async () => {
  try {
    const $ = await scrape('https://httpbin.org/json');
  } catch(error) {
    console.error(error.message);

    if (error.hasOwnProperty('response')) {
      // The response body.
      console.error(error.response.body);
    }
  }
})();
```

### Scraping Multiple Pages

```js
const scrape = require('./src');

(async () => {
  const $ = await Promise.all([
    scrape('https://example.com'),
    scrape('https://httpbin.org/html')
  ]);

  console.log({
    heading_1: $[0]('h1').text(),
    heading_2: $[1]('h1').text()
  });
})();
```

## License

[MIT](https://github.com/risan/page-scraper/blob/master/LICENSE) © [Risan Bagja Pradana](https://bagja.net)

---
_Source: https://npm.io/package/page-scraper · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
