Created
January 28, 2009 04:09
-
-
Save danchoi/53805 to your computer and use it in GitHub Desktop.
yelp neighborhoods to yaml
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
#!/usr/bin/env ruby | |
require 'rubygems' | |
require 'hpricot' | |
require 'open-uri' | |
url = "http://www.yelp.com/developers/documentation/neighborhood_list" | |
doc = open(url) {|f| Hpricot(f.read)} | |
def nest(li_elem) | |
next_sibling = li_elem.next_sibling | |
if next_sibling && next_sibling.name == "ul" | |
label = li_elem.inner_text | |
{label => next_sibling.children_of_type("li").map {|e| nest(e)}} | |
else | |
li_elem.inner_text | |
end | |
end | |
ul_root = doc.at("ul.attr_list") | |
result = ul_root.children_of_type("li").map do |country| | |
nest(country) | |
end | |
puts result.to_yaml |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment