用groovy采集网页数据

首先,用 http://groovyconsole.appspot.com/ 测试下面的代码,发现引用总是失败.

下载了GGTS: https://spring.io/tools/ggts

测试成功: 

NewImage

 

@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 {it.@class == '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].a.@href.text().trim(),
Team: it.td[2].text().trim(),
Pos: it.td[3].text().trim()
]
}

data.each { it -> println it }
}
}
}


本文转自疯吻IT博客园博客,原文链接:http://www.cnblogs.com/fengwenit/p/5620484.html如需转载请自行联系原作者

上一篇:阿里巴巴统一运维智能化平台演进之路


下一篇:《OpenACC并行程序设计:性能优化实践指南》一 导读