Skip to content

Instantly share code, notes, and snippets.

@suwa320
Created November 9, 2012 13:19
Show Gist options
  • Select an option

  • Save suwa320/4045636 to your computer and use it in GitHub Desktop.

Select an option

Save suwa320/4045636 to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
require 'open-uri'
require 'nokogiri'
url = "http://ja.wikipedia.org/wiki/国際電話番号の一覧"
dom = Nokogiri(open(URI.encode(url)).read)
callcode = []
dom.search('table.wikitable tr').each do |record|
head = record.search('th')
data = record.search('td')
next unless head.size == 1 and data.size == 2
code = head.first.inner_text
country = data.first.inner_text
unless code.empty? or country.empty?
callcode << {
:code => code.gsub(/\D+/, '').to_i,
:country => country.gsub(/\[[^\]]+\]/, '').strip}
end
end
# csv
callcode.each do |v|
puts "%d,%s" % [v[:code], v[:country]]
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment