本帖最后由 zhupenghe 于 2018-11-20 12:00 编辑
简单用Python写个查询快递的程序,由于是新手刚学还请大神勿喷哦!
还正在学习的朋友让我们一起学习进步加油吧! 我用到的是最新的requests_html 库 安装方法 pip install requests_html
下面直接附上源代码: 大家可以根据情况把代码优化一下哦! 谢谢大家!
[Python] 纯文本查看 复制代码 from requests_html import HTMLSession
session = HTMLSession()
def sb(dh):
'''用来识别快递 返回快递种类!'''
url = 'http://www.kuaidi100.com/autonumber/autoComNum?resultv2=1&text=' + dh
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'
}
r = session.get(url, headers=header)
r = r.json()
return r['auto'][0]['comCode']
def kuaidi100(dh):
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36'
}
url = 'http://www.kuaidi100.com/query'
data = {
'type': sb(dh),
'postid': dh,
'temp': '0.916395518853228'
}
r = session.get(url, params=data)
r = r.json()
#print(r)
for x in r['data']:
print(x['ftime'] + '-' * 7 + x['context'])
if __name__ == '__main__':
print('*' * 20 + '在线实时快递查询系统' + '*' * 20)
dh = str(input('请输入快递单号:'))
kuaidi100(dh)
|