上篇文章Swift 最简单的方式来解析HTML我通过截取字符串的方式,很艰难的实现了一个解析HTML的方式,但是过程相当难受,并且扩展性不强,适应性也很糟糕。
libxml2
libxml2的官网在这里:http://xmlsoft.org/html/index.html,当你想通过官网去学习libxml2的话,我觉得会相当困难,因为它只有头文件已经简单的函数描述,所以在这里:Matt Gallagher,它写了一个包装器使我们使用libxml2更方便一些。
Using libxml2 for XML parsing and XPath queries in Cocoa它写的一篇文章,有兴趣的可以看一下。
MWResolver
MWResolver是我在libxml2上进行的一层Swift版本的封装,可以通过纯Swift的方式来解析HTML/XML,先看一下需求:
还是以SwiftDoc为例,我们从源码中,想获取到所有的标签li
里面的第一行的内容(AnyBidirectionalCollection),用MWResolver
这么写就可以了:
let resolver = MWResolver(data: data)
guard let element = resolver.peekParse(query: "//div[@class='col-sm-6']//li//a") else { return }
print(element.content)
// 打印:AnyBidirectionalCollection
有了这个神器,可以说什么网站网页都不怕了,下面我来一个比较完整的实例,解析SwiftDoc
:
- 首先我们先拿到所有的标题:
let resovler = MWResolver(data: data)
let elementH2s = resovler.parse(query: "//article[@class='content']//h2")
for ele in elementH2s {
print(ele.content)
}
// 打印:
Types
Protocols
Operators
Globals
- 为了便于查看我们拿最少的Globals下的所有函数:
let elementLis = resovler.parse(query: "//li/a")
var contents = [String]()
for ele in elementLis {
guard let content = ele.attributes["href"] else { return }
if content.hasPrefix("/v3.1/func/") {
contents.append(content)
print(content)
}
}
// 打印:
/v3.1/func/abs
/v3.1/func/assert
/v3.1/func/assertionFailure
/v3.1/func/debugPrint
/v3.1/func/dump
/v3.1/func/fatalError
/v3.1/func/getVaList
/v3.1/func/isKnownUniquelyReferenced
/v3.1/func/max
/v3.1/func/min
/v3.1/func/numericCast
/v3.1/func/precondition
/v3.1/func/preconditionFailure
/v3.1/func/print
/v3.1/func/readLine
/v3.1/func/repeatElement
/v3.1/func/sequence
/v3.1/func/stride
/v3.1/func/swap
/v3.1/func/transcode
/v3.1/func/type
/v3.1/func/unsafeBitCast
/v3.1/func/unsafeDowncast
/v3.1/func/withExtendedLifetime
/v3.1/func/withUnsafeBytes
/v3.1/func/withUnsafeMutableBytes
/v3.1/func/withUnsafeMutablePointer
/v3.1/func/withUnsafePointer
/v3.1/func/withVaList
/v3.1/func/withoutActuallyEscaping
/v3.1/func/zip
也就是对应的SwiftDoc中这些数据:
如何使用:
- 下载MWResolver
- 把上面三个文件导入到项目中。
- 导入libxml2
- 在header search path 中 + ${SDK_ROOT}/usr/include/libxml2
- 引入桥文件支持OC+Swift混编
github地址:https://github.com/hellolad/MWResolver
如果解决了您的问题,请记得给个star