Si-World

vichare的硅基世界

从Google weather api读取天气预报的python脚本

September15
?View Code PYTHON
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import urllib
from xml.dom import minidom
 
proc = lambda tnlist, conds : [ \
    [temp.getAttribute('data') for temp in conds.getElementsByTagName(tagname)][0] \
    for tagname in (tnlist) ]
 
data = urllib.urlopen('http://www.google.com/ig/api?weather=Beijing&hl=zh-cn').read().decode('gbk').encode('utf8')
xmldoc = minidom.parseString(data)
curconds = xmldoc.getElementsByTagName("current_conditions")
(t, h, c, w) = proc(('temp_c', 'humidity', 'condition', 'wind_condition'), curconds[0])
print t
print h
print c
print w
 
for cond in xmldoc.getElementsByTagName("forecast_conditions"):
    (d, l, h, c) = proc(('day_of_week', 'low', 'high', 'condition'), cond)
    print '%s: %s/%s %s' % (d, l, h, c)
posted under 我就不分类

Email will not be published

Website example

Your Comment: