![158c03c115e5eb4fa47dced721cb9036.gif](https://i-blog.csdnimg.cn/blog_migrate/2c76855abd4d2e71089356fabd8ab10a.gif)
点击关注,我们共同每天进步一点点!
文件上传
在做接口自动化的时候,有时需要上传文件,比如更改头像等等,在request里,通过files参数来上传
import requests
base_url = 'http://httpbin.org'
file = {'file': open(r'E:\00.jpg', 'rb')}
r = requests.post(base_url + '/post', files=file)
print(r.text)
文件下载
第一种方式
import requests
def dowload_file(file_path):
headers = {"Referer": "https://xx315.xx315.nex"}
cookie = {"Cookie": "ASP.NET_SessionId=bij"}
r = requests.get(url='https://xx315.xx315',
cookies=cookie,
headers=headers,
stream=True)
if r.status_code == 200:
with open(file_path, 'wb') as f:
for chunk in r.iter_content(chunk_size=1024):
f.write(chunk)
dowload_file('F:\\123.xlsx')
注意:
文件如果不存在,会在当前目录下生成一个文件,有内容会清空在写入
第二种方式
import requests
import shutil
def download_file_raw(file_path):
url = 'https://xx315.xx315.net/Ashx/Export'
cookie = {"Cookie": 'ASP.NET_SessionId=sjl8'}
r = requests.get(url=url,
cookies=cookie,
stream=True
)
if r.status_code == 200:
with open(file_path, 'wb') as f:
r.raw.decode_content = True
shutil.copyfileobj(r.raw, f)
download_file_raw('F:\\123.xlsx')
原文地址:
https://www.cnblogs.com/zouzou-busy/p/11407709.html
![f1c3567c341433dd08b06f9d40fa5f1f.png](https://i-blog.csdnimg.cn/blog_migrate/a2b38c3f13463e8b7093853bbce45fa8.jpeg)
![f5f18af379cc2750f7cda0d4323ebc56.gif](https://i-blog.csdnimg.cn/blog_migrate/4adeab9fb9c06457412a029c8f7c3eeb.gif)
喜欢请关注,有用请转发~
升职、加薪、无漏测-点“在看”
![17a7ec34e9b424533c8e01a2b65c4027.gif](https://i-blog.csdnimg.cn/blog_migrate/a2040d0e479e47b2eb8c9f851e90e576.gif)