new_beautiful_soup_dart 1.0.0 copy "new_beautiful_soup_dart: ^1.0.0" to clipboard
new_beautiful_soup_dart: ^1.0.0 copied to clipboard

Dart native package inspired by Beautiful Soup 4 Python library. Provides easy ways of navigating, searching, and modifying the HTML tree.

new_beautiful_soup_dart #

pub package

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
    • Going up
      • .parent
      • .parents
    • Going sideways
      • .nextSibling and .previousSibling
      • .nextSiblings and .previousSiblings
    • Going back and forth
      • .nextElement and .previousElement - Returns the next/previous Bs4Element.
      • .nextElements and .previousElements
      • .nextParsed and .previousParsed - Returns the next/previous parsed Node (doc comments, tags, text). To get its data as a String, use node.data.
      • .nextParsedAll and .previousParsedAll
  • Searching the tree
    • findFirstAny() - Returns the top-most (first) element of the parse tree, of any tag type.
    • findAll()
    • find()
    • findParents() and findParent()
    • findNextSiblings() and findNextSibling()
    • findPreviousSiblings() and findPreviousSibling()
    • findAllNextElements() and findNextElement()
    • findAllPreviousElements() and findPreviousElement()
    • findNextParsedAll() and findNextParsed()
    • findPreviousParsedAll() and findPreviousParsed()
  • Modifying the tree
    • Changing tag names and attributes
    • Modifying .string
    • append()
    • extend()
    • newTag()
    • insert()
    • insertBefore() and insertAfter()
    • 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.
    • .text and getText()

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.

1
likes
160
points
6
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Dart native package inspired by Beautiful Soup 4 Python library. Provides easy ways of navigating, searching, and modifying the HTML tree.

Repository (GitHub)
View/report issues

License

MIT (license)

Dependencies

html

More

Packages that depend on new_beautiful_soup_dart