new_beautiful_soup_dart
Dart native package inspired by the Beautiful Soup 4 Python library. It provides easy ways for navigating, searching, and modifying HTML trees.
Usage
A simple usage example:
import 'package:new_beautiful_soup_dart/new_beautiful_soup_dart.dart';
// 1. Parse a document String
BeautifulSoup bs = BeautifulSoup(html_doc_string);
// Use BeautifulSoup.fragment(html_doc_string) if you parse a partial HTML snippet
// 2. Navigate quickly to any element
bs.body!.a!; // Navigate quickly with tags. Use outerHtml or toString to get outer html
bs.find('p', class_: 'story'); // Finds the first element with html tag "p" and which has "class" attribute with value "story"
bs.findAll('a', attrs: {'class': true}); // Finds all elements with html tag "a" and which have a defined "class" attribute with whatever value
bs.find('', selector: '#link1'); // Find with custom CSS selector (other parameters are ignored)
bs.find('*', id: 'link1'); // Any element with id "link1"
bs.find('*', regex: r'^b'); // Find any element which tag starts with "b", for example: body, b, ...
bs.find('p', string: r'^Article #\d*'); // Find "p" element which text starts with "Article #[number]"
bs.find('a', attrs: {'href': 'http://example.com/elsie'}); // Finds by "href" attribute
// 3. Perform other actions for the navigated element
Bs4Element bs4 = bs.body!.p!; // Navigate quickly with tags
bs4.name; // Get tag name
bs4.string; // Get text
bs4.toString(); // Get String representation of this element, same as outerHtml
bs4.innerHtml; // Get html elements inside the element
bs4.className; // Get class attribute value
bs4['class']; // Get class attribute value
bs4['class'] = 'board'; // Change class attribute value to 'board'
bs4.children; // Get all element's children elements
bs4.replaceWith(otherBs4Element); // Replace with other element
bs4.wrap('div'); // Wrap the element inside a newly created 'div' tag
bs4.smooth(); // Consolidate adjacent text nodes inside the element
bs4.prettify(); // Output the element using a cleanly formatted and indented HTML string
// ... and many more
Check the test directory for more complete examples.
Table of Contents
- Navigating the tree
- Going down
- Navigating using tag names
.contentsand.children.descendants.string.stringsand.strippedStrings
- Going up
.parent.parents
- Going sideways
.nextSiblingand.previousSibling.nextSiblingsand.previousSiblings
- Going back and forth
.nextElementand.previousElement- Returns the next/previousBs4Element..nextElementsand.previousElements.nextParsedand.previousParsed- Returns the next/previous parsed Node (doc comments, tags, text). To get its data as a String, usenode.data..nextParsedAlland.previousParsedAll
- Going down
- Searching the tree
findFirstAny()- Returns the top-most (first) element of the parse tree, of any tag type.findAll()find()findParents()andfindParent()findNextSiblings()andfindNextSibling()findPreviousSiblings()andfindPreviousSibling()findAllNextElements()andfindNextElement()findAllPreviousElements()andfindPreviousElement()findNextParsedAll()andfindNextParsed()findPreviousParsedAll()andfindPreviousParsed()
- Modifying the tree
- Changing tag names and attributes
- Modifying .string
append()extend()newTag()insert()insertBefore()andinsertAfter()clear()extract()decompose()replaceWith()wrap()- Optionally accepts a String representation of a tag.unwrap()smooth()- Consolidates adjacent text nodes.
- Output
prettify()- Outputs recursively indented HTML representation..textandgetText()
Other methods from the Element class in the html package can be accessed via bs4element.element.
Features and Bugs
Please file feature requests and bugs at the issue tracker or feel free to raise a pull request.