JSSoup

by chishui

chishui /JSSoup

JavaScript + BeautifulSoup = JSSoup

161 Stars 14 Forks Last release: Not found 28 Commits 0 Releases

Available items

No Items, yet!

The developer of this repository has not created any items for sale yet. Need a bug fixed? Help with integration? A different license? Create a request here:

JSSoup

I'm a fan of Python library BeautifulSoup. It's feature-rich and very easy to use. But when I am working on a small react-native project, and I tried to find a HTML parser library like BeautifulSoup, I failed.
So I want to write a HTML parser library which can be so easy to use just like BeautifulSoup in Javascript.
JSSoup uses tautologistics/node-htmlparser as HTML dom parser, and creates a series of BeautifulSoup like API on top of it.
JSSoup supports both node and react-native.

Build Status npm version NPM

Naming Style

JSSoup tries to use the same interfaces as BeautifulSoup so BeautifulSoup user can use JSSoup seamlessly. However, JSSoup uses Javascript's camelCase naming style instead of Python's underscore naming style. Such as

find_all()
in BeautifulSoup is replaced as
findAll()
.

Install

$ npm install jssoup 

How to use JSSoup

Import

//react-native
import JSSoup from 'jssoup'; 
// nodejs
var JSSoup = require('jssoup').default;

Make Soup

var soup = new JSSoup('hello');

The text element only contains whitespace will be ignored by default. To disable this feature, set second parameter of JSSoup to false. This parameter is "ignoreWhitespace" and will be passed into htmlparser.

javascript
var soup = new JSSoup('hello', false);

Name

var soup = new JSSoup('hello');
var tag = soup.find('head');
tag.name
// 'head'
tag.name = 'span'
console.log(tag)
//hello

Attributes

var soup = new JSSoup('hello');
var tag = soup.nextElement;
tag.attrs
// {id: 'hi', class: 'banner'} 
tag.attrs.id = 'test';
console.log(tag)
// hello

Navigation

.previousElement, .nextElement

var data = `
1 2 3
` var soup = new JSSoup(data); var div = soup.nextElement; var b = div.nextElement.nextElement; // b.string: '2' var a = b.previousElement; // a.string: '1'

.previousSibling, .nextSibling

var soup = new JSSoup(data);
var div = soup.nextElement;
var a = div.nextElement;
var b = a.nextSibling;
var c = b.nextSibling;
c.nextSibling == undefined;

.contents

div.contents
// [1, 2, 3]

.descendants

div.descendants
// [1, 1, 2, 2, 3, 3]

.parent

div.parent == soup

Edit

.extract()

b.extract();
div.contents
// [1, 3]

.append()

b.extract();
div.append(b)
div.contents
// [1, 3, 2]

.insert(position, new Element)

d.prettify('', '')
// 4
div.insert(1, d)
div.contents
// [1, 4, 2, 3]

.replaceWith(new Element)

d.prettify('', '')
// 4
b.replaceWith(d)
div.contents
// [1, 4, 3]

c.string.replaceWith('new') div.contents // [1, 4, new]

Search

.findAll()

var data = `

`
var soup = new JSSoup(data);
soup.findAll('a')
// [hello]
soup.findAll('div', 'h1')
// [
]

.find()

var data = `

hello

world

` var soup = new JSSoup(data); soup.find('p') //

hello

.findNextSibling()

var data = `
test
div

hello

world

` var soup = new JSSoup(data); var span = soup.find('span'); span.findNextSibling('p') //

hello

.findNextSiblings()

var data = `
test
div

hello

world

` var soup = new JSSoup(data); var span = soup.find('span'); span.findNextSiblings('p') //

hello

//

world

.findPreviousSibling()

var data = `

hello

world

div
test
` var soup = new JSSoup(data); var span = soup.find('span'); span.findPreviousSibling('p') //

world

.findPreviousSiblings()

var data = `

hello

world

div
test
` var soup = new JSSoup(data); var span = soup.find('span'); span.findPreviousSiblings('p') //

hello

//

world

Output

.prettify()

var soup = new JSSoup('hello');
soup.prettify()
// 
//  
//   hello
//  
// 

.getText(), .text

div.text
// '123'
div.getText('|')
// '1|2|3'

.string

b.string == '2';
var soup = new JSSoup('hello');
soup.string == 'hello';

Run Test

npm test

Status

There's a lot of work need to be done.

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.