以前用易语言写过这个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[i]['comCode'],
- 'postid' : self.postID,
- 'temp' : '0.916395518853228'
- }
- res = requests.get(url,params=data).text
- pMsg = re.findall(r'message":"(.*?)"',res)[0]
- if pMsg =='ok':
- right_data = re.findall(r'data":(.*?\])',res)[0]
- 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)[0]
- data_json = json.loads(data)
- return data_json
- if __name__ =='__main__':
- p = EXPRESS100('4000000705') #输入快递单号
- res = p.getPostMsg()
- pprint.pprint(res)
复制代码
|