Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/chishui/jssoup
JavaScript + BeautifulSoup = JSSoup
https://github.com/chishui/jssoup
beautifulsoup crawler html javascript nodejs parser react-native spider
Last synced: 3 days ago
JSON representation
JavaScript + BeautifulSoup = JSSoup
- Host: GitHub
- URL: https://github.com/chishui/jssoup
- Owner: chishui
- Created: 2017-06-08T21:08:27.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-03-04T02:33:03.000Z (almost 2 years ago)
- Last Synced: 2024-05-02T04:53:33.385Z (8 months ago)
- Topics: beautifulsoup, crawler, html, javascript, nodejs, parser, react-native, spider
- Language: JavaScript
- Homepage:
- Size: 262 KB
- Stars: 364
- Watchers: 15
- Forks: 37
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
Awesome Lists containing this project
README
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](https://github.com/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**.[![unit-tests](https://github.com/chishui/JSSoup/workflows/unit-tests/badge.svg)](https://github.com/chishui/JSSoup/actions)
[![npm version](https://badge.fury.io/js/jssoup.svg)](https://badge.fury.io/js/jssoup)
[![NPM](https://img.shields.io/npm/dm/jssoup.svg)](https://www.npmjs.com/package/jssoup)# 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
```javascript
//react-native
import JSSoup from 'jssoup';
// nodejs
var JSSoup = require('jssoup').default;
```
## Make Soup
```javascript
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);
```
## Access Element Attributes
### Name
```javascript
var soup = new JSSoup('hello');
var tag = soup.find('head');
tag.name
// 'head'
tag.name = 'span'
console.log(tag)
//hello
```
### Attributes
```javascript
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
```javascript
var data = `
`
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
```javascript
var soup = new JSSoup(data);
var div = soup.nextElement;
var a = div.nextElement;
var b = a.nextSibling;
var c = b.nextSibling;
c.nextSibling == undefined;
```
### .previousSiblings, .nextSiblings
```javascript
var soup = new JSSoup(data);
var a = soup.find("a");
a.nextSiblings
// [2, 3]
var c = soup.find("c");
c.previousSiblings
// [1, 2]
```
### .contents
`.contents` contains direct children of current element.
```javascript
div.contents
// [1, 2, 3]
```
### .descendants
`.descendants` includes all elements of which current element is the ancestor of.
```javascript
div.descendants
// [1, 1, 2, 2, 3, 3]
```
### .parent
```javascript
div.parent == soup
```
## Edit
### .extract()
```javascript
b.extract();
div.contents
// [1, 3]
```
### .append()
```javascript
b.extract();
div.append(b)
div.contents
// [1, 3, 2]
```
### .insert(position, new Element)
```javascript
d.prettify('', '')
// 4
div.insert(1, d)
div.contents
// [1, 4, 2, 3]
```
### .replaceWith(new Element)
```javascript
d.prettify('', '')
// 4
b.replaceWith(d)
div.contents
// [1, 4, 3]c.string.replaceWith('new')
div.contents
// [1, 4, new]
```## Search
### .findAll()
```javascript
var data = `
`
var soup = new JSSoup(data);
soup.findAll('a')
// [hello]
soup.findAll('div', 'h1')
// []
```
### .find()
```javascript
var data = `
hello
world
`
var soup = new JSSoup(data);
soup.find('p')
//hello
```
### .findNextSibling()
```javascript
var data = `
test
div
hello
world
`
var soup = new JSSoup(data);
var span = soup.find('span');
span.findNextSibling('p')
//hello
```
### .findNextSiblings()
```javascript
var data = `
test
div
hello
world
`
var soup = new JSSoup(data);
var span = soup.find('span');
span.findNextSiblings('p')
//hello
//world
```
### .findPreviousSibling()
```javascript
var data = `
hello
world
div
test
`
var soup = new JSSoup(data);
var span = soup.find('span');
span.findPreviousSibling('p')
//world
```
### .findPreviousSiblings()
```javascript
var data = `
hello
world
div
test
`
var soup = new JSSoup(data);
var span = soup.find('span');
span.findPreviousSiblings('p')
//hello
//world
```
## CSS Selector
JSSoup utilizes [JSSoupSelector](https://github.com/chishui/JSSoupSelector) for CSS selector functionalities.
### .select
```javascript
var data = `
hello
world
div
test
`
var soup = new JSSoup(data);
var elements = soup.select('div > .class1');
elements
// [hello
,div]
```
### .selectOne
```javascript
var data = `
hello
world
div
test
`
var soup = new JSSoup(data);
var element = soup.selectOne('div > p#id1');
element
//hello
```
## Output
### .prettify()
```javascript
var soup = new JSSoup('hello');
soup.prettify()
//
//
// hello
//
//
```
### .getText(), .text
```javascript
div.text
// '123'
div.getText('|')
// '1|2|3'
```
### .string
```javascript
b.string == '2';
var soup = new JSSoup('hello');
soup.string == 'hello';
```# Run Test
```
npm test
```