普贤教程 › 包参考
htmlparse 包
HTML 解析(子集)
| 项 | 值 |
|---|---|
| 包名 | htmlparse |
| 版本 | 0.2.0 |
| 可用版本 | 0.1.0 / 0.2.0 |
| 入口 | htmlparse.px |
| 分类 | 数据格式与编解码 |
| 文件数 / 行数 | 1 / 315 |
安装与引入
bash
pxpkg init # 首次 pxpkg add htmlparse@0.2.0 pxpkg install
px
import htmlparse所属分类教程:数据格式与编解码
公开接口
| 函数 | 参数 |
|---|---|
hp_parse | s |
hp_find | el, tag |
hp_text | el |
hp_attr | el, name |
其余 16 个 def(内部与辅助,可直接调用但不属稳定接口)
| 函数 | 参数 |
|---|---|
hp_trim | s |
hp_find_from | s, sub, start |
hp_is_space | c |
hp_tag | raw |
hp_void | tag |
hp_find_rec | el, tag, out |
hp_decode_entities | s |
hp_find_by_attr | el, tag, attr_name, attr_value |
hp_find_by_attr_rec | el, tag, attr_name, attr_value, out |
hp_find_first | el, tag |
hp_inner_html | el |
hp_outer_html | el |
hp_table_parse | el |
hp_extract_links | el |
hp_extract_images | el |
hp_strip_tags | s |
包内说明(源码头注释)
text
============================================================
普贤生态库:registry/htmlparse —— HTML 解析(子集)
------------------------------------------------------------
纯函数库(无 IO、无状态),编译(px build)与解释(px run)双模式一致。
hp_parse(s) → Ok(root dict) | Err
元素 = {tag, attrs: dict, children: [元素|文本], parent};
root.tag = "#root"。注释跳过;void 标签(br/img/input/…)不嵌套;
属性支持 name="v" / name='v' / name(布尔,值为 "")。
hp_find(el, tag) → [元素](DFS,大小写不敏感)
hp_text(el) → str(递归拼接文本)
hp_attr(el, name) → string | null(缺省 null)
说明:非严格容错(未闭合标签按栈顶弹出处理;不校验证实/实体解码)。
============================================================