前景:工作中需要批量造数据,而这个数据是通过上传xlsx文件方式,如下图,我想要用python自动调用生成批量数据


?
解决问题:
抓包分析下

?python使用第三方包解决
pip install requests-toolbelt
三方包介绍:The Requests Toolbelt — requests_toolbelt 0.8.0 documentation
代码实例
import mimetypes
from requests_toolbelt import MultipartEncoder
import requests
def get_content_type(filename):
"""
获取文件的content-type的方法
:param filename:
:return:
"""
return mimetypes.guess_type(filename)[0] or 'application/octet-stream'
m = MultipartEncoder(
fields={'dataType': 'CASE',
'inputBatchCode': 'hfww1',
'templateId': '103',
'templateName': '全量模板',
'upload': '1',
'file': ('1646829122279.xlsx',
open(r"D:\1646829122279.xlsx", "rb"),
get_content_type(r"D:\1646829122279.xlsx"))}
)
headers = {"Content-Type": m.content_type,
"X-Token": "meQhbfyqJhP9PzHxW4e4rWjQXwHs1xAJMjGLxPvQuHO7X0dm",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Safari/537.36"
}
res = requests.post(url="上传的url",
headers=headers,
data=m)
print(res.status_code)
print(res.content.decode("utf-8"))

?
|