-
-
Save txominpelu/4e3298486901a905c5ef to your computer and use it in GitHub Desktop.
Web Scraping with groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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