截获圆通快递官网物流信息查询 API 并实现代码

最近几天都在用圆通快递官网查快递,感觉甚至麻烦,于是就有了这篇文章。

我们用 Microsoft Edge 访问圆通快递官网 https://www.yto.net.cn/ ,按住 Ctrl + Shift + I 打开“开发人员工具”,输入运单号,点击查询,等待页面加载完成,此时,在右侧的“网络”选项卡中找到 waybill:

image

点一下:

image

这样,就能获取到网站发送的请求了,方法为 POST, URL 为 https://www.yto.net.cn/api/trace/waybill ,我们再往下滑到最底部:

image

这里就是 POST 的数据了。
至此,可以开始写代码了。在代码库里翻了半天,试图想用 Ctrl + C / V 大法完成代码的编写,哎,你说巧不巧,还真有,修改完代码,运行一下,报错了:

发生异常: SSLError
HTTPSConnectionPool(host='***.***.***.***', port=***): Max retries exceeded with url: /***/***/*** (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1123)')))

During handling of the above exception, another exception occurred:


During handling of the above exception, another exception occurred:

  File "E:\Workplace\VisualStudioCode\***.py", line *, in <module>
    r = requests.post(url, data = data)

找到如下解决方案:https://www.pythonheidong.com/blog/article/600579/373c5474d5bcada35c3e/ ,一字未改,覆盖,跑一下,报错了,再次寻找答案:https://maenze.com/archives/1082 ,加以修改,真不错,成了。

贴上最终的源码(根据 https://www.pythonheidong.com/blog/article/600579/373c5474d5bcada35c3e/ 修改):

import time
import requests

word = input("请输入你要查询的单号:")
if word.startswith("YT") or word.startswith("yt") :
	url = "https://www.yto.net.cn/api/trace/waybill"
	data = {"waybillNo":word}
	res = requests.post(url, data = data, verify = True)
	js = res.json()
	track = js["data"][0]["traces"]
	for i in track:
		timeTemp = float(i["time"] / 1000) #时间戳转换
		tupTime = time.localtime(timeTemp) #时间戳转换
		stadardTime = time.strftime("%Y-%m-%d %H:%M:%S", tupTime) #时间戳转换
		print(stadardTime + ":" + i["info"])
else :
	print("")
posted @ 2021-07-14 20:25  摘叶飞镖  阅读(436)  评论(0编辑  收藏  举报