Created
October 4, 2012 09:16
-
-
Save eladkehat/3832450 to your computer and use it in GitHub Desktop.
Get the list of top sites for a given country from Alexa
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
# Get the list of top sites for a given country from Alexa, found here: | |
# http://www.alexa.com/topsites/countries/<country code> | |
# and transform it into a nice text file, one domain per line. | |
# Supply the country code as the first argument. Defaults to US. | |
require 'rubygems' | |
require 'open-uri' | |
require 'nokogiri' | |
BASE_URL = "http://www.alexa.com/topsites/countries" | |
country = ARGV[0] || "US" | |
def parse_page(url, outfile) | |
doc = Nokogiri::HTML(open(url)) | |
domains = doc.css(".topsites-label").map(&:inner_text) | |
domains.each {|domain| outfile.puts domain } | |
end | |
outfile = File.open(ARGV[1] || "alexatop500.#{country}.txt", 'w') | |
0.upto(20) do |page| | |
url = BASE_URL + (page > 0 ? ";#{page}" : "" ) + "/#{country}" | |
puts "Parsing page #{page + 1} at #{url}" | |
parse_page(url, outfile) | |
end | |
outfile.close | |
puts "Written to #{outfile.path}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment