代码如下,调用方式 http://127.0.0.1:8888/kuaidi?id=JD0061102228400&ph=6896
多次刷新 json 就会重复数据(一次就不会),如何让它不重复?(一白一枚,代码有点烂~)
import re
import time
import random
import requests_html
session = requests_html.HTMLSession()
kuaidiList = []
class Kuaidi(object):
def __init__(self, number, phone):
# 转运信息 url
self.url = 'https://www.kuaidi100.com/query'
# 获取 token url 和 Hm_lvt
self.token_url = 'https://www.kuaidi100.com/'
# 获取快递名 url
self.name_url = 'https://www.kuaidi100.com/autonumber/autoComNum'
# 快递名参数
self.number_params = {
'text': number
}
self.token_params = {
'from': 'openv'
}
# 转运信息参数
self.temp = str(random.random())
self.params = {
'type': self.getname(),
'postid': number,
'temp': self.temp,
'phone': ''
}
csrftoken = self.gettoken()
self.headers = {
'Connection': 'keep-alive',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.193 Safari/537.36',
'Referer': 'https://www.kuaidi100.com/?from=openv',
'Cookie': 'csrftoken=' + csrftoken[0] + '; Hm_lvt_' + csrftoken[1] + '=' + str(
int(time.time())) + '; Hm_lpvt_' + csrftoken[1] + '=' + str(int(time.time()))
}
# 获取转运信息
def getdata(self):
# 转运信息参数
rous = session.get(self.url, headers=self.headers, params=self.params)
print(rous.text)
datas = rous.json()['data']
for data in datas:
kuaidiList.append(data)
# 获取 token 和 Hm_lvt
def gettoken(self):
rous = session.get(self.token_url, params=self.token_params)
Set_Cookie = rous.headers['Set-Cookie']
csrftoken = re.findall(', csrftoken=(.*?);', Set_Cookie)[0]
js_url = re.findall('https://cdn.kuaidi100.com/js/share/count.js(.*)">', rous.text)[0]
url = 'https://cdn.kuaidi100.com/js/share/count.js' + js_url
rous = session.get(url)
Hm_lvt = re.findall('https://hm.baidu.com/hm.js\?(.*)";', rous.text)[0]
return [csrftoken, Hm_lvt]
# 获取快递名
def getname(self):
rous = session.post(self.name_url, params=self.number_params)
name = rous.json()['auto'][0]['comCode']
return name
import flask, json
from flask import request
'''
flask:web 框架,通过 flask 提供的装饰器 @server.route()将普通函数转换为服务
登录接口,需要传 url 、username 、passwd
'''
# 创建一个服务,把当前这个 python 文件当做一个服务
server = flask.Flask(__name__)
# server.config['JSON_AS_ASCII'] = False
# @server.route()可以将普通函数转变为服务 登录接口的路径、请求方式
@server.route('/kuaidi', methods=['get', 'post'])
def login():
# 获取通过 url 请求传参的数据
username = request.values.get('id')
print(username)
# 获取 url 请求传的密码,明文
pwd = request.values.get('ph')
print(pwd)
# 判断用户名、密码都不为空,如果不传用户名、密码则 username 和 pwd 为 None
if username and pwd:
Kuaidi(number=username, phone=pwd).getdata()
if kuaidiList:
resu = {'code': 200, 'message': '查询成功'}
return json.dumps(kuaidiList, ensure_ascii=False) # 将字典转换为 json 串, json 是字符串
else:
resu = {'code': -1, 'message': '没有查到结果'}
return json.dumps(resu, ensure_ascii=False)
else:
resu = {'code': 10001, 'message': '参数不能为空!'}
return json.dumps(resu, ensure_ascii=False)
if __name__ == '__main__':
# http://localhost:8888/kuaidi?id=JD0061102228400&ph=6896
server.run(debug=True, port=8888, host='0.0.0.0') # 指定端口、host,0.0.0.0 代表不管几个网卡,任何 ip 都可以访问