python 获取天气【转载】

本文转自:http://dango-akachan.appspot.com/?p=98001 
写下代码,以便自己查阅:
import urllib2
import os
from xml.dom.minidom import parseString

url="http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getWeatherbyCityName?theCityName=北京"
#url="http://www.webxml.com.cn/webservices/weatherwebservice.asmx/getSupportProvince?"
request = urllib2.Request(url)
response =urllib2.urlopen(request)

#print response.geturl().decode('utf-8').encode('gbk')
xmlstring = response.read()
#print xmlstring #查看xml
dom = parseString(xmlstring)

strings = dom.getElementsByTagName("string")
 
def getText(nodelist):
    rc = ""
    for node in nodelist:
        if node.nodeType == node.TEXT_NODE:
            rc = rc + node.data
    return rc
 
#今天的温度和天气
today_temperature=getText(strings[5].childNodes)
today_weather=getText(strings[6].childNodes)
 
#明天的温度和天气
tomorrow_weather=getText(strings[13].childNodes)
tomorrow_temperature=getText(strings[12].childNodes)
 
#省份城市
province = getText(strings[0].childNodes)
city = getText(strings[1].childNodes)
weather=today_weather+" " +today_temperature+"\n" +tomorrow_weather+" "+tomorrow_temperature
print province, city
print weather
os.system("pause")

#加上最后一个暂停,放在桌面上,用这个脚本查询天气足以。

posted on 2012-04-18 21:24 DjvuLee 阅读(340) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


导航

<2012年4月>
25262728293031
1234567
891011121314
15161718192021
22232425262728
293012345

统计

常用链接

留言簿

随笔分类(13)

随笔档案(19)

文章分类(2)

文章档案(1)

搜索

最新评论

评论排行榜