Created
February 17, 2012 03:36
-
-
Save fooqri/1850373 to your computer and use it in GitHub Desktop.
Ruby Nokogiri usage example
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
# Code Snipet for demonstrating the use of nokogiri | |
# The code will look up yor retired racing greyhound by its racing name | |
# And return info about your dog as well as a list of its litter mates. | |
# | |
require 'rubygems' | |
require 'nokogiri' | |
require 'open-uri' | |
require 'uri' | |
# Use the dog's name for lookup of dog information | |
dogname = ARGV[0] || "Noble Sky" | |
get_dog_info_url = "http://www.trackinfo.com/dog.jsp?runnername=" + URI.escape(dogname) | |
dog_doc = Nokogiri::HTML(open(get_dog_info_url)) | |
# Get the left ear tag which is all the litter id for identifying all litter mates. | |
left_ear_tag = dog_doc.css("tr:nth-child(2) .it2 a").text | |
name = dog_doc.css("h1").text | |
right_ear_tag = dog_doc.css("tr:nth-child(2) .it4").text | |
starts = dog_doc.css(".detail-wrap tr:nth-child(4) td:nth-child(2)").text | |
dob = dog_doc.css(".it4 em").text | |
sex_color = dog_doc.css(".it2 em").text | |
sex = sex_color[0,1] | |
color = sex_color[6,4] | |
sire = dog_doc.css("tr:nth-child(1) .it2 a").text | |
dam = dog_doc.css(".it4 a").text | |
# Look up all litter mates | |
get_litter_info_url = "http://www.trackinfo.com/dog-search.jsp?keyword=" + URI.escape(left_ear_tag) + "&by=ltattoo" | |
litter_doc = Nokogiri::HTML(open(get_litter_info_url)) | |
puts("Your Dog: ", dogname, "has the following siblings:") | |
# Parse the resulting document for key data elements. | |
# Then print the info to the console. | |
litter_doc.css(".item-table tbody tr").each do |tr| | |
if tr.css(".name").text.casecmp(dogname) != 0 | |
puts(tr.css(".name").text) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment