写法一:
import requests
#图片链接
image_url= "https://www.shuquge.com/files/article/image/3/3478/3478s.jpg"
headers ={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
}
r = requests.get(image_url,headers=headers)
# 下载图片
# 二进制数据需要用r.content 进行提取
# 将图片放在‘图库’文件夹下,‘图库’是文件夹的名称,将图片放入该文件夹中,该文件夹与py文件在同一目录下
f = open("图库/ye.jpg",'wb')
f.write(r.content)
f.close()
写法二:
import requests
#图片链接
image_url= "https://www.shuquge.com/files/article/image/3/3478/3478s.jpg"
headers ={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.45 Safari/537.36'
}
r = requests.get(image_url,headers=headers)
# 下载图片
with open("图库/ye.jpg" ,mode = "wb") as f:
f.write(r.content) #图片内容写入文件