スクリプト
ボタンポンで、gCommunicationのサイトからとってきてKMLを出力するようにしました。
汚くてすいません。特に、HTML抽出の正規表現が恥ずかしくて死ねます。
require 'hpricot' require 'open-uri' require 'kconv' require 'rexml/document' $KCODE = "UTF8" class Geocoding KEY = '' # ここは、自分のKEYを入れてください http://code.google.com/apis/maps/signup.html # "My web site URL"には、http://localhost/ を入れてください。 GEOCODING = 'maps.google.com' METHOD = "/maps/geo?output=xml&key=#{KEY}&q=" def self.get(adr) request_url = URI.encode(METHOD+ adr) Net::HTTP.start(GEOCODING) {|http| res = http.get(request_url, {"referer" => "http://localhost/"}) return res.body.toutf8 } end end def dputs(str) # terminal code is sjis STDERR.puts str.to_s.tosjis end NOVA_LIST = 'http://www.g-com.jp/1106nova/open/index.html' doc = Hpricot(open(NOVA_LIST).read) xmldoc = nil cnt = 1 (doc/:table/:tr).each {|elem| t = elem.containers.last next unless t adr = "" ary = t.to_plain_text.split("\n") ary.each {|r| r = r.toutf8 adr += r.chomp.gsub(/(.*$/, "").gsub(/^[ \t ]+/, " ") if r.include?('(') adr.sub!(' ', ' ') adr.gsub!(/^ /, "") adr.gsub!(/[ ]*$/, "") adr.gsub!(/[ ][^ ]+$/, "") adr.sub!(' ', '') break end } next if /img/ === adr next if /COPYRIGHT/ === adr next if /G.communication/ === adr next if /開校済み/ === adr next if /電話番号/ === adr res = Geocoding.get(adr) if xmldoc == nil xmldoc = REXML::Document.new res xmldoc.root.elements.delete(xmldoc.root.elements[1]) end x = REXML::Document.new res res_xml = REXML::XPath.first(x, "//Response") name = res_xml.elements["name"] begin res_xml.elements["Placemark"].elements << name ary = REXML::XPath.match(res_xml, "//Placemark") raise "cannot geocode" if ary.size == 0 ary[0].elements << name if ary.size > 1 then ary[1..ary.size-1].each{|a| res_xml.elements.delete a } end xmldoc.root.elements << res_xml cnt += 1 rescue dputs "ERR: " + URI.decode(name.text) next end #break if cnt > 5 } puts xmldoc.to_s
われながら汚すぎるソースです。気が向いたら直します・・・