一個輕量級的 Flutter/Dart 套件,用於透過抓取標籤及其屬性來提取 HTML 內容,並轉換為結構化的 Dart 清單與映射。非常適合解析靜態網頁或從 HTML 源取得資料,而無需進行完整的 DOM 操作。它支援多種平台,包括行動裝置、Web 與桌面端,使其成為需要從 HTML 回應中提取結構化資料的跨平台應用程式的多功能選擇。
適用情境
從靜態網頁中提取數據
解析 HTML 表格和列表
從網頁內容中抓取元數據
從HTML來源填入本地資料
構建簡單的網路爬蟲
主要特色
用於HTML標籤提取的簡單API
支援屬性和文字內容的檢索
將抓取的資料轉換為 Dart 列表和映射
跨平台相容性
輕量且易於整合
適合對象
開發數據獲取應用的開發者
需要HTML解析但不使用重型框架的項目
原型設計網路爬蟲功能
需要最少伺服器端處理的應用程式
使用建議
不適用於動態 JavaScript 渲染的內容
需要有效的HTML輸入
可能無法優雅地處理錯誤格式的HTML
僅限靜態頁面抓取
依賴外部HTML來源的可靠性
原始 README
以下為關聯原始碼儲存庫的 README 原文快照,最新內容請造訪原始碼儲存庫。
A Simple Web Scraper for Dart & Flutter
A very basic web scraper implementation to scrap html elements from a web page.
Pull requests are most welcome.
Getting Started
In your pubspec.yaml root add:
dependencies:web_scraper:
then,
import'package:web_scraper/web_scraper.dart';
Note that as of version 0.0.6, the project supports not only Flutter projects, but also Dart projects.
Implementation
final webScraper = WebScraper('https://webscraper.io');
if (await webScraper.loadWebPage('/test-sites/e-commerce/allinone')) {
List<Map<String, dynamic>> elements = webScraper.getElement('div.thumbnail > div.caption', ['h4']);
print(elements);
}
Loads the webpage into response object and then parse it into the document object
String route
Future <bool>
loadFromURL
Loads the webpage from provided URL into response object and then parse it into the document object
String page
Future <bool>
loadFromString
Loads the webpage from a String (usually stored by the getPageContent method) into response object and then parse it into the document object. This operation is completely synchronous and exists as a helper method to perform compute() flutter operations and avoid jank
String responseBodyAsString
Future <bool>
getPageContent
Returns webpage's html in string format
Void
String body
getElement
Returns List of elements found at specified address
String address, List <String> attributes
List <Map<String, dynamic>>
getElementTitle
Returns List of element titles found at specified address
String address
List <String>
getElementAttribute
Returns List of elements single attribute found at specified address (if you wish to get multiple attributes at once, please use getElement instead)
String address, List <String> attributes
List <String>
getAllScripts
Returns the list of all data enclosed in script tags of the document
Void
List <String>
getScriptVariables
Returns Map between given variable names and list of their occurence in the script tags
List <String> variableNames
Map <String, dynamic>
Contributing
Please branch from develop to implement bug fix/new feature.
Ensure that code is formatted according to base dart rules & using the latest stable version of dart.
Open a PR with develop as the PR target with a clear description.