首先,用?http://groovyconsole.appspot.com/?测试下面的代码,发现引用总是失败.
下载了GGTS:?https://spring.io/tools/ggts
测试成功:?
?
@Grapes( @Grab(group=‘org.ccil.cowan.tagsoup‘, module=‘tagsoup‘, version=‘1.2‘) )
import org.ccil.cowan.tagsoup.Parser;
class TestHtml {
public static void main(String[] args){
String ENCODING = "UTF-8"
def PARSER = new XmlSlurper(new Parser() )
def url = "http://www.nfl.com/stats/categorystats?archive=false&conference=null&statisticCategory=PASSING&season=2010&seasonType=REG&experience=null&tabSeq=0&qualified=true&Submit=Go"
new URL(url).withReader (ENCODING) { reader ->
def document = PARSER.parse(reader)
def data = document.‘**‘.find {[email protected] == ‘data-table1‘}.tbody.tr.collect {
[
Rk: it.td[0].text().trim(),
Player: it.td[1].text().trim(),
PlayerUrl: "http://www.nfl.com" + it.td[1][email protected]().trim(),
Team: it.td[2].text().trim(),
Pos: it.td[3].text().trim()
]
}
data.each { it -> println it }
}
}
}