最近初学python,进展到网络爬虫部分,尝试自己写一下查票,结果遇到了麻烦
import requests
url = 'https://kyfw.12306.cn/otn/leftTicket/queryZ?' \
'leftTicketDTO.train_date=2020-02-01&' \
'leftTicketDTO.from_station=SHH&' \
'leftTicketDTO.to_station=BJP&purpose_codes=ADULT'
result=requests.get(url)
print(result.json())
出现的问题如下:
Traceback (most recent call last):
File "D:/Files/check tickets/123.py", line 7, in <module>
print(result.json())
File "D:\Files\python3\lib\site-packages\requests\models.py", line 897, in json
return complexjson.loads(self.text, **kwargs)
File "D:\Files\python3\lib\json\__init__.py", line 357, in loads
return _default_decoder.decode(s)
File "D:\Files\python3\lib\json\decoder.py", line 337, in decode
obj, end = self.raw_decode(s, idx=_w(s, 0).end())
File "D:\Files\python3\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)
不知如何解决,求解答,谢谢!
抓包看下,返回的数据不是标准的json,可能是返回了错误提示信息,如果那样,先看下提示了什么。12306经常升级,你直接拷贝以前别人写的代码肯定不行,建议你抓包以后根据浏览器的提交,自己写,最好用 webdriver 这种模拟浏览器的方案。
返回的数据是html文本格式,不是类似于字典格式的文本数据,查看结果应该用result.content.decode('utf-8')
把最下面的改成print(result.text)
另外还需要加载cookies 否则网页会出现 无法访问网络