Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Web Scraping with groovy
import org.ccil.cowan.tagsoup.Parser;
String ENCODING = "UTF-8"
@Grapes( @Grab(group='org.ccil.cowan.tagsoup', module='tagsoup', version='1.2') )
def PARSER = new XmlSlurper(new Parser() )
def url = "http://www.bing.com/search?q=web+scraping"
new URL(url).withReader (ENCODING) { reader ->
//JQuery Selector : $('#results h3 a').each(function(index){alert ($(this).text())})
def document = PARSER.parse(reader)
println "\nExample 1:\n"
document.'**'.find{ it['@id'] == 'results'}.ul.li.div.div.h3.a.each { println it.text() }
println "\nExample 2\n"
document.'**'.find{ it['@id'] == 'results'}.'**'.findAll{ it.name() == 'h3'}.a.each { println it.text() }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment