Skip to content

Instantly share code, notes, and snippets.

@bfb
Created October 15, 2012 00:06
Show Gist options
  • Save bfb/3890224 to your computer and use it in GitHub Desktop.
Save bfb/3890224 to your computer and use it in GitHub Desktop.
require 'mechanize'
require 'httparty'
require 'csv'
all_images = []
a = Mechanize.new
p = a.get("http://flagcounter.com/countries.html")
all = p.root.css(".post > table").children().to_s.split("actbook/")
all.each do |x|
all_images << "http://flagcounter.com/factbook/#{x[0..1]}" unless x[0..1] == "<t"
end
CSV.open(File.expand_path("flags.csv"), "wb") do |csv|
all_images.each do |x|
puts "GET=>#{x}"
page = a.get(x)
name = page.root.css(".post > h1").text.split("\n").first.strip
puts name.inspect
url_image = page.root.css(".post > table").first.children.last.children.css("img").last.attributes["onclick"].to_s.gsub("this.src='", "").split("';").first
puts "IMAGE=>#{url_image.inspect}"
csv << [url_image, name]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment