问题描述
在 Jenkins Pipeline 中,我们需要对 HTML 文档进行多种操作。之前使用 XMLParser 及 XMLSlurper 库操作 HTML 文档时遇到很多问题,因为 HTML 文档结构松散(比如没有结束标签等等),不是标准的 XML 文件,因此导致 XML 解析失败。
该笔记将整理:在 Groovy 中,操作 HTML 文档的常用方法
解决方案
方案一、使用 Jsoup 类库
方案二、使用 TagSoup 类库
我们未验证该方法,只是阅读 Grape 手册时发现该方法,这里仅简单记录:
// find the PDF links of the Java specifications
@Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2.1')
def getHtml() {
def parser = new XmlParser(new org.ccil.cowan.tagsoup.Parser())
parser.parse("https://docs.oracle.com/javase/specs/")
}
html.body.'**'.a.@href.grep(~/.*\.pdf/).each{ println it }
方案一、使用 Jsoup 类库
简单的入门示例
@Grab(group='org.jsoup', module='jsoup', version='