requests 发送文件

创建日期: 2022-11-26 22:30 | 作者: 风波 | 浏览次数: 14 | 分类: Python

使用 requests 库发送文件

files = {'upload_file': open('file.txt','rb')}

r = requests.post(url, files=files)

requests will send a multi-part form POST body with the upload_file field set to the contents of the file.txt file. The filename will be included in the mime header for the specific field

还可以设置文件内容

files = {'upload_file': ('foobar.txt', open('file.txt','rb'), 'text/x-spam')}

发送文件的时候发送其它参数

files = {'upload_file': open('file.txt','rb')}
values = {'DB': 'photcat', 'OUT': 'csv', 'SHORT': 'short'}

r = requests.post(url, files=files, data=values)

来源:https://stackoverflow.com/questions/22567306/how-to-upload-file-with-python-requests

14 浏览
13 爬虫
0 评论