Python 如何 发送 multiipart 上传文件?

2018-08-23 20:05:02 +08:00
 bestehen
Content-Disposition: form-data; name="_csrf"

pvlVVBZkGPROBe1zHHx17fc5VKtVMcLMGS6TKFn82HPFjgMBeCsvnXtf3SksKELVmm4tziJFipMoSPp3Ds2SQA==
-----------------------------119938005516990736431240663742
Content-Disposition: form-data; name="ReportForm[url]"


-----------------------------119938005516990736431240663742
Content-Disposition: form-data; name="ReportForm[url]"; filename="å¼€å‘çŽ¯å¢ƒå’Œé¢„ä¸Šçº¿çŽ¯å¢ƒçš„ä»£ç åŒæ­¥.pdf"
Content-Type: application/pdf


file=os.getcwd()+'/'+"齐家网 COO 毛新勇:《未来平台赋能是必然,但要真刀真枪才有意义》-18 页.pdf"
files={'ReportForm[url]':open(file,'rb')}
data={'_csrf':"23ccd68ef09d0241c45e60e893f1a6f6095b4d3a925790a2d384e2bb53c2f657a%3A2%3A%7Bi%3A0%3Bs%3A5%3A%22_csrf%22%3Bi%3A1%3Bs%3A32%3A%22vuzdYe35uBps-XPOgKBnTqnIg8bvm9yJ%22%3B%7D"}
response=requests.post(url=url,cookies=cookies,headers=headers,files=files,data=data)
我这样写综述不对呢
1346 次点击
所在节点    问与答
3 条回复
lieh222
2018-08-24 09:36:35 +08:00
response.request.body 打印看看呗
bestehen
2018-08-24 12:46:18 +08:00
@lieh222 返回的是 none
我现在是这样写的
files=[("ReportForm[url]",('齐家网 COO 毛新勇:《未来平台赋能是必然,但要真刀真枪才有意义》-18 页.pdf',open(file,'rb'),'Content-Type: application/pdf'))]
data={"_csrf":csrf_token}
headers=headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36',
}

response=requests.post(url=url,cookies=cookies,files=files,data=data,headers=headers)
print(response.request.body)
bestehen
2018-08-24 15:31:27 +08:00
网页源码是这样的

<form id="w0" class="form-horizontal" action="/report/add?_=1535094880131" method="post" enctype="multipart/form-data">
<input type="hidden" name="_csrf" value="4n_OPKuOaFi3F2pKZZwCPje9xQM4aIwKOZiHWUSoOSGnDqls7uo4HY5aAClc0nVhR_OIMloA-D5L3rYGNMdLVA=="><div class="form-group field-reportform-url">
<label class="control-label col-sm-3" for="reportform-url">上传报告</label>
<div class="col-sm-6">
<input type="hidden" name="ReportForm[url]" value=""><input type="file" id="reportform-url" class="report_pdf_new" name="ReportForm[url]">
<p class="help-block help-block-error "></p>
</div>

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/482641

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX