本文转自:
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")
#加上最后一个暂停,放在桌面上,用这个脚本查询天气足以。