Skip to content

Instantly share code, notes, and snippets.

@txominpelu
Created August 18, 2011 12:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save txominpelu/4e3298486901a905c5ef to your computer and use it in GitHub Desktop.
Save txominpelu/4e3298486901a905c5ef to your computer and use it in GitHub Desktop.
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