Python 3:文件和数字资产/文件/下载
外观
%pip install requests
%pip install pillow
如果您使用的是来自操作系统的命令终端。您可以通过编写以下命令来下载所需的软件包
pip install requests
pip install pillow
from requests import get as http_get_requests
from PIL import Image
from io import BytesIO
url_to_image: str = 'https://upload.wikimedia.org/wikipedia/commons/thumb/3/30/Stepper_Motor%2C_Model_17HS4401N%2C_4.jpg/1024px-Stepper_Motor%2C_Model_17HS4401N%2C_4.jpg'
headers = {
'User-Agent': 'Download tutorial([link]; [email])'
}
response = http_get_requests(
url_to_image,
headers=headers
)
content_type: str = response.headers['content-type']
content_length: int = int(response.headers['content-length'])
# Desired location to save the file
save_at: str = '/mnt/c/Users/Kentv/Desktop/test/test.jpg'
with Image.open(
BytesIO(
response.content
)
) as image:
image.save(save_at)