鱼C论坛

 找回密码
 立即注册
查看: 3493|回复: 2

[已解决]求帮忙关于老师视频里面的天气预报程序

[复制链接]
发表于 2016-8-16 16:24:33 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 fantahzcn 于 2016-8-18 13:51 编辑

各位大神:

我在看老师视频里面的一个天气预报程序 运行报错, 一开始报错是API地址不对,我做了更新了,但是程序运行还是错误,用3.3.5  和 3.4.3 都不行

3.3.5 错误如下
Python 3.3.5 (v3.3.5:62cf4e77f785, Mar  9 2014, 10:35:05) [MSC v.1600 64 bit (AMD64)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
请输入城市:上海
Traceback (most recent call last):
  File "C:\Python33\lib\json\decoder.py", line 367, in raw_decode
    obj, end = self.scan_once(s, idx)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "E:\学习资料和文档\python\天气\天气查询\天气查询.py", line 11, in <module>
    weatherJSON = json.JSONDecoder().decode(weatherHTML)#创建json
  File "C:\Python33\lib\json\decoder.py", line 351, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python33\lib\json\decoder.py", line 369, in raw_decode
    raise ValueError("No JSON object could be decoded")
ValueError: No JSON object could be decoded
>>>

3.4.3 错误如下
Python 3.4.3 (v3.4.3:9b73f1c3e601, Feb 24 2015, 22:43:06) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
请输入城市:上海
Traceback (most recent call last):
  File "E:/学习资料和文档/python/天气/天气查询/天气查询2.py", line 11, in <module>
    weatherJSON = json.JSONDecoder().decode(weatherHTML)#创建json
  File "C:\Python34\lib\json\decoder.py", line 343, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())
  File "C:\Python34\lib\json\decoder.py", line 361, in raw_decode
    raise ValueError(errmsg("Expecting value", s, err.value)) from None
ValueError: Expecting value: line 1 column 1 (char 0)
>>>

请帮忙下啊////

源码如下
import urllib.request
import json
import pickle

pickle_file=open('city_data.pkl','rb')
city= pickle.load(pickle_file)
password=input('请输入城市:')
name1=city[password]
File1 =urllib.request.urlopen('http://m.weather.com.cn/mweather/'+name1+'.shtml')#打开url
weatherHTML= File1.read().decode('utf-8')#读入打开的url
weatherJSON = json.JSONDecoder().decode(weatherHTML)#创建json
weatherInfo = weatherJSON['weatherinfo']
#打印信息
print ( '城市:', weatherInfo['city'])
print ('时间:', weatherInfo['date_y'])
print ( '24小时天气:')
print ('温度:', weatherInfo['temp1'])
print ('天气:', weatherInfo['weather1'])
print ('风速:', weatherInfo['wind1'])
print ('紫外线:', weatherInfo['index_uv'])
print ('穿衣指数:', weatherInfo['index_d'])
print ('48小时天气:')
print ('温度:', weatherInfo['temp2'])
print ('天气:', weatherInfo['weather2'])
print ('风速:', weatherInfo['wind2'])
print ('紫外线:', weatherInfo['index48_uv'])
print ('穿衣指数:', weatherInfo['index48_d'])
print ('72小时天气:')
print ('温度:', weatherInfo['temp3'])
print ('天气:', weatherInfo['weather3'])
print ('风速:', weatherInfo['wind3'])
input ('按任意键退出:')
最佳答案
2017-1-8 18:12:23
你取的数据不是JSON格式,所以没有办法解析。
你取的数据类似如下格式:
<!DOCTYPE HTML>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

<link type="text/css" rel="stylesheet" >

<script type="text/javascript" src="http://i.tq121.com.cn/j/jquery-1.8.2.js?20140529_105543"></script>

<script>

$(function(){

$(".nav").toggle(

        function () {

       



        $("#layout").addClass("active");

         }, function () {

       

        $("#layout").removeClass("active");



        })

        var dataUrl = "http://d1.weather.com.cn/dingzhi/_id_.html";//加载城市信息地址

                var dataUrls = "http://d1.weather.com.cn/aqi_7d/XiangJiAqiFc5d/_id_.html";

        //异步初始化预警信息

        var reg = /\d{9}/;

        var localId = document.URL.match(reg)[0];

        $.getScript(dataUrl.replace('_id_',localId),function(){

                var arr = eval("alarmDZ"+localId).w;

                if(arr.length>0){

                        $(".yj").show();

                        //var proId = localId.substring(0,5);

                        //var $h1 = $('.sk h1');

                        //var strA = '<a target="_blank" href="http://www.weather.com.cn/alarm/newalarmlist.shtml?prov='+proId+'"><i></i><em>'+arr.length+'个预警中</em></a>';

                        //var strA=arr;

                        //$h1.append(strA);       

                       

                        var txt = '';

                         $.each(arr,function(idx,item){  

                            if(item.w11.length < 10){

                                         txt += '<li><img src="http://i.tq121.com.cn/i/wap/alarm_s/'+item.w4+item.w6+'.gif"><a href="/malarm/alarmcontent.shtml?file='+item.w11+'"">'+item.w1+item.w2+item.w3+'发布'+item.w5+item.w7+'预警</a></li>';

                                 }else{

                                          txt += '<li><img src="http://i.tq121.com.cn/i/wap/alarm_s/'+item.w4+item.w6+'.gif"><a href="/malarm/alarmcontent.shtml?file='+item.w11+'">'+item.w1+item.w2+item.w3+'发布'+item.w5+item.w7+'预警</a></li>';

                                 }

                        })

                        $('.yj ul').html(txt).show();

                               

                }else{

                        $(".yj").hide();

                }

        })

        $.getScript(dataUrls.replace('_id_',localId),function(){

               

               

               

                for(var i=0;i<5&&i<json['2001006'].length;i++){

                                var datavalue=json['2001006'][i]['002'];

                                var $apiarr=json['2001006'][i]['001'];

                               

                                if($apiarr>0 && $apiarr<50)

city_data.zip

30.46 KB, 下载次数: 53

city

小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2016-8-18 07:35:04 | 显示全部楼层
city_data.pkl 传个附件吧
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2017-1-8 18:12:23 | 显示全部楼层    本楼为最佳答案   
你取的数据不是JSON格式,所以没有办法解析。
你取的数据类似如下格式:
<!DOCTYPE HTML>

<html>

<head>

<meta charset="utf-8">

<meta name="viewport" content="width=device-width,initial-scale=1.0, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">

<link type="text/css" rel="stylesheet" >

<script type="text/javascript" src="http://i.tq121.com.cn/j/jquery-1.8.2.js?20140529_105543"></script>

<script>

$(function(){

$(".nav").toggle(

        function () {

       



        $("#layout").addClass("active");

         }, function () {

       

        $("#layout").removeClass("active");



        })

        var dataUrl = "http://d1.weather.com.cn/dingzhi/_id_.html";//加载城市信息地址

                var dataUrls = "http://d1.weather.com.cn/aqi_7d/XiangJiAqiFc5d/_id_.html";

        //异步初始化预警信息

        var reg = /\d{9}/;

        var localId = document.URL.match(reg)[0];

        $.getScript(dataUrl.replace('_id_',localId),function(){

                var arr = eval("alarmDZ"+localId).w;

                if(arr.length>0){

                        $(".yj").show();

                        //var proId = localId.substring(0,5);

                        //var $h1 = $('.sk h1');

                        //var strA = '<a target="_blank" href="http://www.weather.com.cn/alarm/newalarmlist.shtml?prov='+proId+'"><i></i><em>'+arr.length+'个预警中</em></a>';

                        //var strA=arr;

                        //$h1.append(strA);       

                       

                        var txt = '';

                         $.each(arr,function(idx,item){  

                            if(item.w11.length < 10){

                                         txt += '<li><img src="http://i.tq121.com.cn/i/wap/alarm_s/'+item.w4+item.w6+'.gif"><a href="/malarm/alarmcontent.shtml?file='+item.w11+'"">'+item.w1+item.w2+item.w3+'发布'+item.w5+item.w7+'预警</a></li>';

                                 }else{

                                          txt += '<li><img src="http://i.tq121.com.cn/i/wap/alarm_s/'+item.w4+item.w6+'.gif"><a href="/malarm/alarmcontent.shtml?file='+item.w11+'">'+item.w1+item.w2+item.w3+'发布'+item.w5+item.w7+'预警</a></li>';

                                 }

                        })

                        $('.yj ul').html(txt).show();

                               

                }else{

                        $(".yj").hide();

                }

        })

        $.getScript(dataUrls.replace('_id_',localId),function(){

               

               

               

                for(var i=0;i<5&&i<json['2001006'].length;i++){

                                var datavalue=json['2001006'][i]['002'];

                                var $apiarr=json['2001006'][i]['001'];

                               

                                if($apiarr>0 && $apiarr<50)
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

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

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2026-2-22 06:12

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

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