找回密码
 立即注册

扫一扫,访问微社区

QQ登录

只需一步,快速开始

查看: 4102|回复: 0

[求助] json decode失败

1

主题

1

帖子

1

积分

贫民

积分
1
FrankLei 发表于 2017-1-13 15:23:10 | 显示全部楼层 |阅读模式
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)

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表