您的位置:首页 > 编程语言 > Python开发

python3获取天气信息

2014-08-30 16:44 357 查看
通过对http://www.webxml.com.cn/WebServices/WeatherWebService.asmx  的调用,获取天气信息

#coding:utf-8
import urllib.parse
import urllib.request
from xml.dom.minidom import parseString
url = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx/getWeatherbyCityName?theCityName"
values = {"theCityName":u"承德"}
data = urllib.parse.urlencode(values).encode(encoding='UTF8')
req = urllib.request.Request(url, data)
response = urllib.request.urlopen(req)
the_page = response.read().decode("utf8")
dom = parseString(the_page)
string = dom.getElementsByTagName("string")
for s in string:
try:
data = s.childNodes[0].data
print(data)
except IndexError as e:
next运行结果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python urlib xml