python+requests——高級用法——上傳文件


import requests

url = 'http://httpbin.org/post'

wj = {'file':open('C:\\Users\\del\\Desktop\\新建文件夾\\1.jpg','rb')}

resp = requests.post(url,files = wj)

print(resp.text)

 

print(type(open('C:\\Users\\del\\Desktop\\新建文件夾\\1.jpg','rb')))   #返回結果:<class '_io.BufferedReader'>

上傳文件只需要指定post方法的files參數即可,files參數的值可以是BufferedReader對象,該對象可以用python語言的內置函數open返回。


定義要上傳的文件,字段中必須有一個key為file的值,值類型是BufferedReader,可以用open函數返回。


post方法將上次的文件轉換為base64編碼形式。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM