用groovy采集网页数据

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

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

测试成功: 

用groovy采集网页数据

 

@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 }
}
}
}

上一篇:Java注解基本原理


下一篇:一个我经常用到的采集网页数据抓取网页获取数据的PHP函数类