已有184人关注
尝试用response.json()返回车票查询信息报错
发表在Python图书答疑 2020-02-12 《零基础学Python》第14章 网络爬虫开发
是否精华
版块置顶:
import  requests
url='https://kyfw.12306.cn/otn/leftTicket/queryO?leftTicketDTO.train_date=2020-02-12&leftTicketDTO.from_station=WXH&leftTicketDTO.to_station=SZH&purpose_codes=ADULT'
response = requests.get(url)
result = response.json()
result = result['data']['result']
print(result)

报错信息为

Traceback (most recent call last):

  File "<ipython-input-37-d25573701c55>", line 1, in <module>
    runfile('C:/Users/wangx/Desktop/14/check tickets/untitled4.py', wdir='C:/Users/wangx/Desktop/14/check tickets')

  File "C:\Users\wangx\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 786, in runfile
    execfile(filename, namespace)

  File "C:\Users\wangx\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 110, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Users/wangx/Desktop/14/check tickets/untitled4.py", line 12, in <module>
    result = response.json()

  File "C:\Users\wangx\Anaconda3\lib\site-packages\requests\models.py", line 897, in json
    return complexjson.loads(self.text, **kwargs)

  File "C:\Users\wangx\Anaconda3\lib\json\__init__.py", line 348, in loads
    return _default_decoder.decode(s)

  File "C:\Users\wangx\Anaconda3\lib\json\decoder.py", line 337, in decode
    obj, end = self.raw_decode(s, idx=_w(s, 0).end())

  File "C:\Users\wangx\Anaconda3\lib\json\decoder.py", line 355, in raw_decode
    raise JSONDecodeError("Expecting value", s, err.value) from None

JSONDecodeError: Expecting value


分享到:
精彩评论 4
无语_mrkj
学分:3155 LV11
2020-02-13
沙发

从错误提示上看应该是数据格式不正确,检查一下数据格式设置是否符合json的要求。另外,也可能是没有爬取到想要的数据。建议按书中介绍的分析方法一步一步分析一下。

三生石
学分:2171 LV10
TA的每日心情
11
2018-12-20 21:21:04
2020-02-13
板凳

您好读者!介于疫情现在12306网页不稳定,所以建议读者近期不要爬取12306网页!

小毛驴1988
学分:45 LV2
2020-02-15
地板
在response = requests.get(url) 后执行print(response)

执行结果为<Response [200]>

执行print(requests.get(url).content.decode('utf-8'))

执行结果为

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>铁路客户服务中心</title>
<link href="/mormhweb/images/global.css" rel="stylesheet" type="text/css">
<link href="/mormhweb/images/err.css" rel="stylesheet" type="text/css"/>
<style type="text/css">
<!--
html,body{background:#fff;}


-->
</style>


</head>

<body>
<div class="err_text">
                                                 <ul id="error" >
                         <li id="err_top">
                         
                         </li>
                                                  <li id="err_bot">  网络可能存在问题,请您重试一下!
                                                                                                  
                                                                                                  <div class="time">                                             <SCRIPT language="javascript">
<!--
function initArray() {
        for (i = 0; i < initArray.arguments.length; i++)
                this[i] = initArray.arguments[i];
}
var isnMonths = new initArray("1月", "2月", "3月", "4月", "5月", "6月", "7月", "8月", "9月", "10月", "11月", "12月");
var isnDays = new initArray("星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期日");
today = new Date();
hrs = today.getHours();
min = today.getMinutes();
sec = today.getSeconds();
clckh = "" + ((hrs > 12) ? hrs - 12 : hrs);
clckm = ((min < 10) ? "0" : "") + min;
clcks = ((sec < 10) ? "0" : "") + sec;
clck = (hrs >= 12) ? "下午" : "上午";
var stnr = "";
var ns = "0123456789";
var a = "";

function getFullYear(d) {
        yr = d.getYear();
        if (yr < 1000)
                yr += 1900;
        return yr;
}

document.write(getFullYear(today) + "年" + isnMonths[today.getMonth()] + today.getDate() + "日" + " " + clckh + ":" + clckm + ":" + clcks + " " +  isnDays[today.getDay()]);

//-->
</SCRIPT></div>
                                                        
                         
                         </li>

                         </ul>

</div>
</body>
</html>


但在浏览器中可见返回的json消息:

EX$Z@{TPJ2L}PUCY77BV4H8.png

U}DF(PK%W}AE351KVPY$0CU.png


无语_mrkj
学分:3155 LV11
2020-02-17
4L

介于疫情现在12306有些车次信息停运,所以建议读者近期不要爬取12306网页!

首页上一页 1 下一页尾页 4 条记录 1/1页
手机同步功能介绍
友情提示:以下图书配套资源能够实现手机同步功能
明日微信公众号
明日之星 明日之星编程特训营
客服热线(每日9:00-17:00)
400 675 1066
mingrisoft@mingrisoft.com
吉林省明日科技有限公司Copyright ©2007-2022,mingrisoft.com, All Rights Reserved长春市北湖科技开发区盛北大街3333号长春北湖科技园项目一期A10号楼四、五层
吉ICP备10002740号-2吉公网安备22010202000132经营性网站备案信息 营业执照