Html 解析器
这是一个用于将 HTML 文档解析为节点和字符串的包。
安装
flutter pub add html_parser_plus
入门
import 'package:html_parser_plus/html_parser_plus.dart';
void main() {
const String htmlString = '''
<html lang="en">
<body>
<div><a href='https://github.com/simonkimi'>author</a></div>
<div class="head">div head</div>
<div class="container">
<table>
<tbody>
<tr>
<td id="td1" class="first1">1</td>
<td id="td2" class="first1">2</td>
<td id="td3" class="first2">3</td>
<td id="td4" class="first2 form">4</td>
<td id="td5" class="second1">one</td>
<td id="td6" class="second1">two</td>
<td id="td7" class="second2">three</td>
<td id="td8" class="second2">four</td>
</tr>
</tbody>
</table>
</div>
<div class="end">end</div>
</body>
</html>
''';
final parser = HtmlParser();
final node = parser.query(htmlString);
parser.parse(node, '//div/a@text');
parser.parse(node,
'//div/a/@href|function:replace(https://,)|function:substring(0,10)');
parser.parseNodes(node, '//tr/td|function:sublist(0,2)');
}
用法
到目前为止,我们通过 xpath_selector 支持了一些 xpath 语法,以及以下三个函数
- sublist 用于
List<XpathNode<Node>> - substring 用于
String - replace 用于
String
您应该知道,在函数中,参数可以被或不被 ‘ 或 “ 包裹。
规则如下://div/a@text|function:repalce(Author,)|function:replace(‘ ‘,”)。
使用 | 来管道所有规则。