前言
本篇主要讲解在python中进行照片获取,使用requests库获取照片可以通过以下步骤实现:
1. 导入requests库:
import requests
2. 使用requests库发送HTTP GET请求获取图片内容:
response = requests.get(image_url)
其中image_url
为图片的URL地址。
3. 将获取到的图片内容保存到文件中:
# 打开名称和方式
img_fil=open('image.jpg','wb')
# 保存地址
img_fil.write(response.content)
其中image.jpg
为保存的文件名,wb
表示二进制写入模式,response.content
返回的是二进制内容。
完整代码示例:
import requests
image_url = 'https://www.example.com/image.jpg'
response = requests.get(image_url)
# 打开名称和方式
img_fil=open('image.jpg','wb')
# 保存地址
img_fil.write(response.content)