本文介绍如何使用requests下载保存图片

一般下载都需要使用到get请求
代码如下:

# coding=utf-8
# 导入requests包
import requests
import time

url = ""		#  下载照片的url
file_path = ""存放照片路径
new = time.strftime('%Y-%m-%d %H_%M_%S', time.localtime(time.time()))	# 当前日期
# 设置照片格式及存放路径
file = file_path + new + "-img.png"
# 打开文件准备写入
r = requests.get(url)
if r.status_code == 200:	# 接口响应200才进行照片下载
       with open(file, "wb") as f:
           for chunk in r.iter_content(128):   # 每到128位就写入
               f.write(chunk)