python requests文件上传

it2022-05-09  26

我们知道requests可以模拟提交一些数据,假如有的网站需要上传文件,可以用它来实现,

例如这里上传的·文件是favicon.ico,自己实现的时候根据自己的实际情况修改

files={'file':open('favicon.ico','rb')} r=requests.post('http://httpbin.org/post',files=files) print(r.text)

运行结果

{ "args": {}, "data": "", "files": { "file": "<此处省略好多" }, "form": {}, "headers": { "Accept": "*/*", "Accept-Encoding": "gzip, deflate", "Content-Length": "151960", "Content-Type": "multipart/form-data; boundary=d62dc620ba7b025c11616f3d526ca08c", "Host": "httpbin.org", "User-Agent": "python-requests/2.22.0" }, "json": null, "origin": "61.184.115.30, 61.184.115.30", "url": "https://httpbin.org/post" }

里面含有·files的字段,而from是空的,说明文件上传的部分会有单独一个files字段来标识

                                                                                                                                               欢迎关注微信公众号  :   码奋

                                                                                                                                               Email:maafenn@gmail.com

                                                                                                                                              


最新回复(0)