Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
J

jsonframe-cheerio

> 编程语言
Open source

simple multi-level scraper json input/output for Cheerio

198 stars0 likes0 views
WebsiteGitHub

About

simple multi-level scraper json input/output for Cheerio

This repository is deprecated. Use it at your own risk!


jsonframe

simple multi-level scraper json input/output

2.0.5x features

JSON Syntax: input json, output the same structured json including with scraped data

Simple patterns: simple inline selectors, extractors, filters and parser.

Reliable & fast: used in production within crawlers

See the full changelog

Example

javascript
let cheerio = require('cheerio')
let $ = cheerio.load(`
	
		
I love jsonframe!

		 Email: [email protected]  
	`)

let jsonframe = require('jsonframe-cheerio')
jsonframe($) // initializing the plugin

let frame = {
	"title": "h1", // this is an inline selector
	"email": "span[itemprop=email] 
{
	"title": "I love jsonframe!",
	"email": "gabin@datascraper.pro"
}
/*

Use

Install the plugin to your Node.js app through NPM

bash
npm i jsonframe-cheerio --save

API

Loading

Start by loading Cheerio.

javascript
let cheerio = require('cheerio')
let $ = cheerio.load("HTML DOM to load") // See Cheerio API

Then load the jsonframe plugin.

javascript
let jsonframe = require('jsonframe-cheerio') // require from npm package
jsonframe($) // apply the plugin to the current Cheerio instance

Scraper

Once the plugin is loaded, you've first got to set the frame of your data.

Let's take the following HTML example:

xml

    
Pricing

		
		A Link
		 We are the 04/02/2017
		

    

        

            Hacker
            Free
              
        
        

            Pro
            $39
              
        
    

	

		Phone FR: +332 38 30 37 90
		Email: [email protected]
	

$( selector ).scrape( frame , {options})

selector is defined in Cheerio's documentation

frame is a JSON or Javascript Object

{options} are detailed later in its own section

javascript
let frame = {
	"title": "h2" // CSS selector
}

We then pass the frame to the function:

javascript
let result = $('body').scrape(frame, { string: true })
console.log( result )
//=> {"title": "Pricing"}

Frame

Inline Selector

Most common selector, inline line by specifying nothing more than the data name property and the selector as its value.

javascript
...
let frame = { "title": "h2" }

let result = $('body').scrape(frame, { string: true })
console.log( result )

/* output =>
	{ "title": "Pricing" }
*/
...

New : Inline attribute / extractor / parser

You can now declare everything in line. You should just be careful to always use them in the following order when combining them : @ (attribute), | (extractor), || (parse).

See examples for each of them above.

Attribute

_a: "attributeName" allows you to retrieve any attribute data
@ inside the selector _s allows you to do it inline

javascript
...
let frame = {
	"proPrice": ".planName:contains('Pro') + span@price"
}

let result = $('body').scrape(frame, { string: true })
console.log( result )

/* output =>
	{ "proPrice": "39.00" }
*/
...

Extractor

` { "email": "[email protected]", "frphone": "33238303790" } */ ...


#### Filter
`|` inside the selector `_s` allows you to do it inline

It currently supports `trim` (remove spaces at beginning and end), `lowercase or lcase`, `uppercase or ucase`, `capitalize or cap`, `words or w`, `noescapchar or nec`, `compact or cmp` and `number or nb`.

```js
...
let frame = {
	"email1": "[itemprop=email] 
	{
		"email1": "[email protected]",
		"email2": "EXAMPLE GOOGLE NET"
	}
*/
...

Parse / Regex

|| inside the selector _s allows you to use regexes in line _p: /regex/ allows you to extract data based on regular expressions

javascript
...
let frame = {
	"data": ".date || \\d{1,2}/\\d{1,2}/\\d{2,4}"
}

// or use the longer version for proper regex entry

let frame = {
	"data": {
		_s: ".date",
		_p: /\d{1,2}\/\d{1,2}\/\d{2,4}/ // n[n]/n[n]/nn[nn] format here
	}
}

let result = $('body').scrape(frame, { string: true })
console.log( result )

/* output =>
	{
		"date": "04/02/2017"
	}
*/
...

List / Array

_d: [{ }] allows you to get an array / list of data
_d: ["selector"] will retrieves a list based on the selector inbetween quotes.
_d: ["firstSelector", "secondSelector"] works too and merge the results into one array

You could even shorten it more by listing right from the selector as follows: "selectorName": [".selector"] which returns an array of strings

javascript
...
let frame = {
	"pricing": {
		_s: "#pricing .item",
		_d: [{
			"name": ".planName",
			"price": ".planPrice"
		}]
	}
}

let result = $('body').scrape(frame, { string: true })
console.log( result )

/* output =>
	{
		"pricing": [
			{
				"name": "Hacker",
				"price": "Free"
			},
			{
				"name": "Pro",
				"price": "$39"
			}
		]
	}
*/

// Or a shorter way which works for simple string arrays

let frame = {
	"pricingNames": ["#pricing .item .planName"]
}

let result = $('body').scrape(frame, { string: true })
console.log( result )

/* output =>
	{
		"pricingNames": ["Hacker", "Pro"]
	}
*/
...

Grouped

"_g": { _s: "", _d: {} } allows you to group some data selectors by a parent selector without naming the parent. You can also extends the group property to add some meaning or simply have several groups at the same level.
Group property name must be _g or _group followed by _ and whatever string you want.
ex: _g_head : {} or _g_body : {}

javascript
...
let frame = {
	_g: {
		_s: "#pricing .item",
		_d: {
			"name": ".planName",
			"price": ".planPrice"
		}
	},
	_g_second: {
		_s: "#pricing .item",
		_d: {
			"secondName": ".planName",
			"secondPrice": ".planPrice"
		}
	}
}

let result = $('body').scrape(frame, { string: true })
console.log( result )

/* output =>
	{
		"name": "Hacker",
		"price": "Free",
		"secondName": "Hacker",
		"secondPrice": "Free"
	}
*/
...

Nested

"parent": { _s: "parentSelector", _d: {} } allows you to segment your data by setting a parent section from which the child data will be scraped.

You can also use "parent": { } when you only want to nest data into objects without setting a parent selector.

javascript
...
let frame = {
	"pricing": {
		_s: "#pricing .item",
		_d: {
			"name": ".planName",
			"price": ".planPrice"
		}
	}
}

let result = $('body').scrape(frame, { string: true })
console.log( result )

/* output =>
	{
		"pricing":{
			"name": "Hacker",
			"price": "Free"
		}
	}
*/
...

Note here that we get the first returned result (#pricing .item).

Example

See how you can properly structure your data, ready for the output!

javascript
...
let frame = {
	"pricing": {
		_s: "#pricing .item",
		_d: [{
			"name": ".planName",
			"price": ".planPrice @ price",
			"image": {
				"url": "img @ src",
				"link": "a @ href"
			}
		}]
	}
}

let result = $('body').scrape(frame, { string: true })
console.log( result )

/* output =>
	{
		"pricing":[
			{
				"name": "Hacker",
				"price": "0",
				"image": {
					"url": "./img/hacker.png",
					"link": "/hacker"
				}
			},
			{
				"name": "Pro",
				"price": "39.00",
				"image": {
					"url": "./img/pro.png",
					"link": "/pro"
				}
			}
		]
	}
*/
...

Note here that we get the first returned result (#pricing .item).

Options

javascript
...
let frame = {
	"proPrice": {
		_s: ".planName:contains('Pro') + span",
		_a: "price"
	}
}

let result = $('body')
	.scrape(frame, {
			timestats: true, // default: false
			string: true // default: false
		})
console.log(result)

/* output =>
	{
		"proPrice": {
			"value":"39.00",
			"_timestats": "1" // ms
		}

	}
*/
...

Tests

One shot tests

bash
npm run test

Watching test on updates

bash
npm run test-watch

Changelog

⚠ Careful if you've been using jsonframe from the version 1.x.x, some things changed to make it more flexible, faster to use (inline parameters) and more meaningful in the syntax.

2.0.52 (28/02/2017)

  • Update the email regex
  • Update the website regex
  • Fix array into array results
  • Improving script efficiency getting data from node(s)
  • Fix date extractor when no date to extract

2.0.51 (27/02/2017)

  • Fix a fatal error (argh) which was just a typo about the new chained extractors

2.0.50 (27/02/2017)

  • Extractors chaining is now possible. For ex: `.selector Feel free to follow the procedure to make it even more awesome!
  1. Create an issue so we get the discussion started
  2. Fork it!
  3. Create your feature branch: git checkout -b my-new-feature
  4. Commit your changes: git commit -am 'Add some feature'
  5. Push to the branch: git push origin my-new-feature
  6. Submit a pull request :D

License

Gabin Desserprit - datascraper.pro
Released under MIT License

Issues· 0 open

View all issuesOpen on GitHub

No open issues yet, or sync has not completed.

> Tags

JavaScriptframejsonparsed-datascraper

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 18, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言