搜索插件、代码仓与 APP 分类

插件详情v0.3.0

beautiful_soup_dart

受 Python 的 Beautiful Soup 4 库启发的 Dart 原生包。提供了一种简单的方式来遍历、搜索和修改 HTML 树。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
2800
喜欢数
113
Pub 分数
160

安装插件

flutter pub add beautiful_soup_dart

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

一个受 Python 的 Beautiful Soup 4 启发的原生 Dart HTML 解析库。它通过简单直观的 API 实现了对 HTML 文档的轻松导航、搜索和修改。非常适合从 HTML 内容中提取数据,或在 Flutter 和其他 Dart 应用程序中操作 DOM 结构。该包支持所有主要平台,包括 Web、移动设备和桌面端。

适合场景

  • Web 抓取 HTML 内容
  • 解析动态HTML响应
  • 从网页中提取结构化数据
  • 在渲染之前修改 HTML
  • 构建静态网站生成器

主要特点

  • 直观的 CSS 和标签选择器
  • 轻松导航父/子关系
  • 支持文本和属性提取
  • 灵活的搜索方法(find、findAll)
  • 跨平台兼容性

适合对象

  • 在Flutter应用中解析HTML的开发者
  • 从网络源提取数据
  • 需要DOM操作的工具
  • 静态站点生成工作流程

使用建议

  • 未针对大型 HTML 文件进行优化
  • 仅限解析,不支持渲染HTML
  • 需要仔细处理格式错误的输入
  • 不支持内置 JavaScript 执行

原始 README

以下为关联源码仓库的 README 原文快照,最新内容请访问源码仓库。

Beautiful Soup Dart

pub package tests codecov

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

Usage

A simple usage example:

import 'package:beautiful_soup_dart/beautiful_soup.dart';

/// 1. parse a document String
BeautifulSoup bs = BeautifulSoup(html_doc_string);
// use BeautifulSoup.fragment(html_doc_string) if you parse a part of html

/// 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 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 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 any 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
... and many more

Check test folder for more examples.

Table of Contents

The unlinked titles are not yet implemented.

Other methods from the Element from 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 PR.

所属分类

依赖

依赖清单 1 项

源码仓库

使用此插件的项目