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

openscraping

v0.3.1

Published

Turn unstructured HTML pages into structured data. The OpenScraping library can extract information from HTML pages using a JSON config file with xPath rules. It can scrape even multi-level complex objects such as tables and forum posts.

Downloads

93

Readme

OpenScraping HTML Structured Data Extraction Node.js library

license:isc Build Status npm package version devDependencies:?

Turn unstructured HTML pages into structured data. The OpenScraping library can extract information from HTML pages using a JSON config file with xPath rules. It can scrape even multi-level complex objects such as tables and forum posts.

This is the Node.js version. A separate but similar C# library is located here.

Self-contained example

First install the package using npm in your project:

npm install openscraping

Then paste this simple example in a js file and run it with node:

var openscraping = require('openscraping')

var config = `
{
  "title": "//h1",
  "body": "//div[contains(@class, 'article')]"
}
`

var html = '<html><body><h1>Article title</h1><div class="article">Article contents</div></body></html>'

var scrapingResults = openscraping.parse(JSON.parse(config), html)

console.log('Extracted title: ' + scrapingResults.title)
console.log('Extracted body: ' + scrapingResults.body)
console.log('Full extracted json: ' + JSON.stringify(scrapingResults))

Here is the output:

Extracted title: Article title
Extracted body: Article contents
Full extracted json: {"title":"Article title","body":"Article contents"}

OpenScraping API Server

If you want to directly run an API server with both a test console UI and an HTTP API, please take a look at the OpenScraping API Server. The API server does not contain a crawler, it just runs rules against HTML sent in with an HTTP POST.

Example: Extracting an article from bbc.com

Below is a simple configuration file that extracts an article from a www.bbc.com page.

{
  "title": "//div[contains(@class, 'story-body')]//h1",
  "dateTime": "//div[contains(@class, 'story-body')]//div[contains(@class, 'date')]",
  "body": "//div[@property='articleBody']"
}

Here is how to call the library:

// config contains the JSON config from above, html contains the HTML we want to extract data from
var openscraping = require('openscraping')
scrapingResults = openscraping.parse(JSON.parse(config), html)
console.log(scrapingResults)

And here is the result for a bbc news article:

{
  title: 'Robert Downey Jr pardoned for 20-year-old drug conviction',
  dateTime: '24 December 2015',
  body: 'Body of the article is shown here'
}

Here is how the www.bbc.com page looked like on the day we saved the HTML for this sample:

Example: Extracting a list of products from Ikea

The sample configuration below is more complex as it demonstrates support for extracting multiple items at the same time, and running transformations on them. For this example we are using a products page from ikea.com.

{
  "products": 
  {
    "_xpath": "//div[@id='productLists']//div[starts-with(@id, 'item_')]",
    "title": ".//div[contains(@class, 'productTitle')]",
    "description": ".//div[contains(@class, 'productDesp')]",
    "price": 
    {
      "_xpath": ".//div[contains(@class, 'price')]/text()[1]",
      "_mapTransformations": [
        "TrimTransformation"
      ]
    }
  }
}

Here is a snippet of the result:

{
  products: [{
    title: 'HEMNES',
    description: 'coffee table',
    price: '$139.00'
  },
...
  {
    title: 'NORDEN',
    description: 'sideboard',
    price: '$149.00'
  },
  {
    title: 'SANDHAUG',
    description: 'tray table',
    price: '$79.99'
  }]
}

Here is how the www.ikea.com page looked like on the day we saved the HTML for this sample:

Map and Reduce Transformations

In the Ikea example above we used a map transformation called TrimTransformation. Transformation modify the raw extracted HTML nodes in some ways. For instance, TrimTransformation just runs str.trim() on the extracted text before it gets written to the JSON output.

The difference between map and reduce transformations is that map transformations act on individual items (for instance on all paragraphs that match the rule //p), while reduce transformations act on the array of extracted items (in our case on all paragraphs). Reduce transformations can be used, for instance, to merge all extracted paragraphs into a single continuous string, because they receive an array of strings as input, and they can choose to return a single string as output.

Built-in map transformations

Name | Purpose | Example -------------------------------------------- | ------- | -------------- ParseDateTransformation | Uses the Date.parse function to parse a string into a date, then converts it back to a string with a certain date format. | Here RemoveExtraWhitespaceTransformation | Replaces consecutive spaces with a single space. For the string "hello world" it would return "hello world". | TrimTransformation | Runs str.trim() on the extracted text before it gets written to the JSON output. | Here TextExtractionBetterWhitespaceTransformation | The default text extractor just calls node.textContent, which often concatenates strings without adding white space between them. This implementation tries to solve this problem by adding extra white spaces in some cases. |

Built-in reduce transformations

Name | Purpose | Example -------------------------------------------- | ------- | -------------- MergeTextArrayIntoSingleText | Expects an array of strings as input, and outputs a single string that concatenates all strings from the input array. Each string is trimmed. If an array item is empty, it is ignored. The transformation concatenates strings using a single space character. | Here