|
楼主 |
发表于 2017-3-2 13:23:42
|
显示全部楼层
改动后源码如下:
# -*- coding:utf-8 -*-
import urllib2
import pickle
import easygui as g
import sys
import json
pickle_file = open('city_data.pkl','rb')
city = pickle.load(pickle_file)
g.msgbox('由于中国天气网接口原因,只能查询到3月4号的天气,请大家谅解~','温馨提示')
#for k,v in city.items():
# print k.decode('utf-8'),v
while 1:
password=g.enterbox(msg='请输入城市:', title=' ', default='', strip=True, image=None, root=None)
print password
name1=city[password.encode('utf-8')] #对取得的password进行utf-8解码
print name1
url='http://www.weather.com.cn/data/sk/'+name1+'.html'
print url #测试
File1 =urllib2.urlopen(url) #打开url
weatherHTML= File1.read().decode('utf-8')#读入打开的url
weatherJSON = json.JSONDecoder().decode(weatherHTML)#创建json
weatherInfo = weatherJSON['weatherinfo']
#打印信息
for k,v in weatherInfo.items(): #测试看取得网页内容,后期键码必须统一
print k.decode('utf-8'),v
print u'所在城市: ', weatherInfo['city']
print u'风 向: ', weatherInfo['WD']
print u'风 速: ', weatherInfo['WSE']
print u'能见度 : ', weatherInfo['njd']
i = g.ccbox('还要查询其它城市吗?',choices=('必须的!','不玩了!'))
if i:
pass
else:
sys.exit(0) |
|