运行一下就卡住退出,我就debug,发现这里返回不了json数据,我把代码单独拎出来测试也是一样,网址单独访问浏览器显示正常,怎么解决?
import requests
import json
def query(date, from_station, to_station):
url = 'https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date={}&leftTicketDTO.from_station={}&leftTicketDTO.to_station={}&purpose_codes=ADULT'.format(date, from_station, to_station)
print(url)
response = requests.get(url)
result = response.json()
print(result)
# result = result['data']['result']
query('2020-09-03', 'SHH', 'BJP')
https://kyfw.12306.cn/otn/leftTicket/query?leftTicketDTO.train_date=2020-09-03&leftTicketDTO.from_station=SHH&leftTicketDTO.to_station=BJP&purpose_codes=ADULT
Traceback (most recent call last):
File "C:/Users/lihui/Desktop/gf.py", line 14, in <module>
query('2020-09-03', 'SHH', 'BJP')
File "C:/Users/lihui/Desktop/gf.py", line 9, in query
result = response.json()
File "D:\Program Files\Python38\lib\site-packages\requests\models.py", line 898, in json
return complexjson.loads(self.text, **kwargs)
File "D:\Program Files\Python38\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "D:\Program Files\Python38\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "D:\Program Files\Python38\lib\json\decoder.py", line 355, in raw_decode
raise JSONDecodeError("Expecting value", s, err.value) from None
json.decoder.JSONDecodeError: Expecting value: line 1 column 1 (char 0)