python 快递100 查询 源码
以前用易语言写过这个post,今天用python实现一下。刚学习python不久,慢慢来吧。
import requests
import json
import re
import pprint
class EXPRESS100:
postID = ''
def __init__(self,data):
self.postID = data
def getPostMsg(self):
url_json = self.geturl()
url_json_num = len(url_json)
""" 检测出正确的快递公司 """
for i in range(url_json_num):
url = 'http://www.kuaidi100.com/query'
data = {
'type' : url_json['comCode'],
'postid' : self.postID,
'temp' : '0.916395518853228'
}
res = requests.get(url,params=data).text
pMsg = re.findall(r'message\":\"(.*?)\"',res)
if pMsg =='ok':
right_data = re.findall(r'data\":(.*?\])',res)
return right_data #得到快递信息,可以自行解析list
def geturl(self):
url = 'http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=' + self.postID
header = {
'Referer':'http://www.kuaidi100.com/',
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
'X-Requested-With':'XMLHttpRequest'
}
datas = {
'resultv2':'1',
'text':self.postID
}
res = requests.post(url,headers=header,data=datas).text
data = re.findall(r'auto":(.*?\])',res)
data_json = json.loads(data)
return data_json
if __name__ =='__main__':
p = EXPRESS100('4000000705') #输入快递单号
res = p.getPostMsg()
pprint.pprint(res)
楼主后面可以不用正则表达式的,可以直接用res.json()返回json数据,这样返回的数据更方便漂亮。
{
"status": "200",
"data": [
{
"ftime": "2018-11-14 08:18:58",
"location": "",
"context": "快件在【石岐】被签收,签收人:【图片签收】",
"time": "2018-11-14 08:18:58"
},
{
"ftime": "2018-11-14 08:05:14",
"location": "",
"context": "到达【石岐】【电话:076088806393】",
"time": "2018-11-14 08:05:14"
},
{
"ftime": "2018-11-14 07:49:11",
"location": "",
"context": "快件在【石岐】卸车,上一站是【虎门分拨中心】,扫描员是【板芙晚班】",
"time": "2018-11-14 07:49:11"
},
{
"ftime": "2018-11-13 23:55:30",
"location": "",
"context": "离开【虎门分拨中心】发往【沙溪中班】",
"time": "2018-11-13 23:55:30"
},
{
"ftime": "2018-11-13 23:55:27",
"location": "",
"context": "到达【虎门分拨中心】",
"time": "2018-11-13 23:55:27"
}
],
"state": "3",
"ischeck": "1",
"message": "ok",
"com": "jinguangsudikuaijian",
"nu": "4000000705",
"condition": "F00"
} 待会试试 学习,谢谢分享! 借鉴一下,准备去扒一些数据,谢谢分享 chenxingg 发表于 2018-11-16 14:10
能查到吗?
当然可以
谢谢分享,已收藏! 感谢分享,学习了。 谢谢分享,已收藏! 一条POST搞定