Skip to content

Instantly share code, notes, and snippets.

@miettal
Created August 26, 2012 06:25
Show Gist options
  • Save miettal/3474999 to your computer and use it in GitHub Desktop.
Save miettal/3474999 to your computer and use it in GitHub Desktop.
PyQueryでYahoo天気をスクレイピングする
# -*- coding: utf-8 -*-
import re
import pyquery
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
def place2weather(place, day="today") :
"""
場所を受け取って,天気を返す関数.
引数:場所の文字列,郵便番号が良い.地名だと失敗すること多し.
返り値:実際の返してくる値を見てください.
"""
urls = []
d = pyquery.PyQuery("http://search.weather.yahoo.co.jp/bin/search?p=%s&s.x=0&s.y=0"%place)
for a in d(".serch-table table tr td a") :
try :
if re.match("http://weather.yahoo.co.jp/weather/(.*)\.html$", a.attrib["href"]) :
urls.append(a.attrib["href"])
except :
pass
weather = {}
d = pyquery.PyQuery(urls[0])
for tr in d("#yjw_pinpoint_today table tr")[1:] :
kind = re.sub(r"\s", "", d(tr)("td")[0].text_content())
weather[kind] = {}
for k,v in zip(d(d("#yjw_pinpoint_%s table tr"%day)[0])("td")[1:],d(tr)("td")[1:]) :
m = re.match(u"^([0-9]*)時$", re.sub(r"\s", "", k.text_content()))
time = u"%02d時"%int(m.group(1))
value = re.sub(r"\s", "", v.text_content())
weather[kind][time] = value
return weather
if __name__ == "__main__" :
post_code = "2900062" #千葉県市原市八幡の郵便番号
print "---今日の天気---"
tenki = place2weather(post_code, "today")
for v in tenki.keys() :
print v
for v2 in sorted(tenki[v].keys()) :
print " %s:%6s"%(v2, tenki[v][v2])
print "---明日の天気---"
tenki = place2weather(post_code, "tomorrow")
for v in tenki.keys() :
print v
for v2 in sorted(tenki[v].keys()) :
print " %s:%6s"%(v2, tenki[v][v2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment