基于libxml2 的 html 解析
- 创建
Document
let doc = Document("path")- 查找,可使用
XPath语法
// 标签名
doc.title()?.name
// 内容
doc.title()?.content
doc.div.div.h2.span["@id='Welcome_to_the_iPhone_Wiki'"]?.content
// 使用 XPath 方式
doc.meta(custom: "[@name='generator']")?.get("content")
// 获取属性
doc.meta[custom: "[@name='generator']"]?.get("content")
doc.meta["@name='generator'"]?.get("content")
doc.html.head.link(["rel": "search", "type": "application/opensearchdescription+xml"])?.get("title")
// 获取当前标签所有属性
doc.html()?.attributes
// 获取当前节点的所有子节点
doc.html()?.children
// 获取当前节点的4个兄弟节点
doc.html.head()?.child?[4]不了解XPath的可以前往w3school学习了解.
- iOS 10.0+
- MacOS 10.12+
- Xcode 11.4+
- Swift 5.2+
.package(url: "https://github.com/podul/XParser", .branch("master"))由于Xcode 11.4才支持import libxml2,所以Xcode最低支持版本为11.4。目前为beta版,需前往开发者网站下载。
Podul, ylpodul@gmail.com
XParser is available under the MIT license. See the LICENSE file for more info.