首先正解错误:请求异常,JSON解码错误:期待值:一行一列(没有字符)。
该问题常见于向某网站进行一些数据的爬取。
错误代码:
url='https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}&leftTicketDTO.to_station={}&purpose_codes=ADULT'.format(date,from_station,to_station)#发送查询请求#url='https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2023-05-12&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=SHH&purpose_codes=ADULT'response=requests.get(url,headers=headers)print(response.text)#将Json数据转换为字典类型,通过键值对获取数据result=response.json()#将数值类型转换为字典类型# result=result['data']['result'] #先获取键"data"对应的值,然后在值中获取键“result”对应的值
为什么出现这种错误,按照报错理解是response中没有json文件类型,(json数据类型类似于python中的字典),实际就是没有读到想要的数据。
错误现象:
é路客æ·æå¡ä¸å¿ headers = {'User-Agent':'你的Agent', 'Cookie':'你的Cookie'}url='https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}&leftTicketDTO.to_station={}&purpose_codes=ADULT'.format(date,from_station,to_station)#发送查询请求#url='https://kyfw.12306.cn/otn/leftTicket/queryZ?leftTicketDTO.train_date=2023-05-12&leftTicketDTO.from_station=BJP&leftTicketDTO.to_station=SHH&purpose_codes=ADULT'response=requests.get(url,headers=headers)print(response.text)#将Json数据转换为字典类型,通过键值对获取数据result=response.json()#将数值类型转换为字典类型result=result['data']['result'] #先获取键"data"对应的值,然后在值中获取键“result”对应的值print(result)
打印的数据:
{"httpstatus":200,"data":{"result":["1erRWLE5DiSTtuO7Sb2VNbRtxfcCXaTV9i0rtlinRlpS4%2BS3jO2c822zpyEqT5x%2FiOwHkblNyK81%0A21thML%2BLxlYjqR6PhcVLThfyHJY%2BrTPRm6dB7u7GjpmGgQHtDYSQAJFNhjNC9%2B1WCr2HQfhT8u1Z%0A3ybyQNPRx2uZc2Bn5tNWj4rMfRraVyl%2BalEIn2k83USKdtI%2Bc9Bcv%2BI1gB529hd%2FVY4bDa7Mjk96%0AUaLcGEf4yjE%2F9QHvFIicdSz9obH5upaAn2AyxatIDZ2eKKcCxnKUATr2ObWcJG80zS7DSfkRfsXT%0A9CVDLpWHIibaJ95toWwiaOWNNBp7699T|预'QTDXxpSETBcW5Py3qi2uXrampAUDpnhUaDGDnL8PmWD4qYrxJQI%2FjelG9UgRhq5rVgzlWIzQ5%2FO0%0AHGz8hfKqer18p8XaEvB%2FuvxYg3pME94eB8mn5mbkiRFUFoygtHNPJxJsl7V%2BPbs8nES2rMLERwwH%0AgrDM8mOeMNAT1Z1frK7gnooBqTsxy%2By3OGZNACR5%2FjH4yUoxw6khDu7KxaWsdkHel45FUQdRa4VD%0A8rSq%2BVqNnaYevCUd2ITuUQ9c9rQxahQGnMNfCaZ2owUWDu9IbdRKuVqxBDsPniko17sQA6kPipgR%0AN65vcBROEacfGcrPo8G7FEAZrdl0ch3C|预订|240000D70505|D705|BJP|SHH|BJP|SHH|21:21|09:27|12:06|Y|a5qynxSNTfuKmJz52ynuwfkuuwVvS39rKLH91S7B5sB3ffqUO8XiBkLnNdU%3D|20230512|3|P4|01|05|1|0||||有|||有||有||有||||I0J0O0W0|IJOO|0|0||I060100021J047400021O031600021O031603036|0|||||1|#1#0#0#z#0|||CHN,CHN||']
完美解决我的问题,希望对此刻苦恼的你有所帮助。