-
-
Save hackhowtofaq/a8e7948eeb9792051fab to your computer and use it in GitHub Desktop.
This file contains hidden or 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
# Example: List of all sports of the Olympic Games | |
# HTML Structure: | |
# | |
# <div id="content"> | |
# <ul> | |
# <li> | |
# <a href="...">SPORT'S NAME</a> | |
# </li> | |
# </ul> | |
# </div> | |
require 'nokogiri' | |
require 'restclient' | |
require 'json' | |
sports = {} | |
BASE_URL = 'http://www.olympic.org' | |
sports_list_url = "#{BASE_URL}/sports" | |
page = Nokogiri::HTML(RestClient.get(sports_list_url)) | |
links = page.css('#content ul li a').select # Select all sport contained in <li> | |
# To format output into JSON | |
links.each do |link| | |
sports[link.text.to_sym] = "#{BASE_URL}/#{link['href']}" | |
end | |
sports.to_json |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment