|
Traceback (most recent call last):
File "C:\Python\WeatherInquirySystem\InquityWeather.py", line 17, in <module>
weatherJSON = json.JSONDecoder().decode(weatherHTML) #创建json
File "C:\Users\Frank_Lei\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 339, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "C:\Users\Frank_Lei\AppData\Local\Programs\Python\Python36\lib\json\decoder.py", line 357, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)
一个获取某个城市的天气的练习题,获取到网页内容后使用json decode 解析失败 错误提示如上。
麻烦啦!各位大神!python版本是3.6
代码如下
import urllib.request
import json
import pickle
import easygui as g
import sys
pickle_file = open('city_date.pkl','rb')
city = pickle.load(pickle_file)
#g.msgbox('由于中国天气网接口原因,只能查询到3月4号的天气,请大家谅解~','温馨提示')
while 1:
#password = g.enterbox(msg='请输入城市:', title=' ', default='', strip=True, image=None, root=None)
name1 = city['广州']
File1 = urllib.request.urlopen('http://www.weather.com.cn/weather1d/'+name1+'.shtml')#打开url
weatherHTML = File1.read().decode('UTF-8') #读入打开的url
#print(weatherHTML)
weatherJSON = json.JSONDecoder().decode(weatherHTML) #创建json
#print(weatherJSON['provinceLinks'])
weatherInfo = weatherJSON['weatherinfo']
#打印信息
information = ''
information = ('城市' + weatherInfo['city'] + '\n' +
'时间:' + weatherInfo['date_y'] + '\n' +
'24小时天气:' + '\n' +
'温度:' + weatherInfo['temp1'] + '\n' +
'天气:'+weatherInfo['weather1'] + '\n' +
'风速:'+ weatherInfo['wind1'] + '\n' +
'紫外线:'+ weatherInfo['index_uv'] + '\n' +
'穿衣指数:', weatherInfo['index_d'])
g.msgbox(information)
i = g.ccbox('还要查询其它城市吗?',choices=('必须的!','不玩了!'))
if i:
pass
else:
sys.exit(0)
|
|