在Python中,您可以使用以下方法下载图片:
urllib.request.urlretrieve
函数:这是Python标准库中的一个函数,可以通过指定URL和文件路径来下载文件。import urllib.request
url = "https://example.com/image.jpg"
file_path = "path/to/save/image.jpg"
urllib.request.urlretrieve(url, file_path)
requests
:requests
库是一个流行的HTTP请求库,可用于发送HTTP请求和处理响应。import requests
url = "https://example.com/image.jpg"
file_path = "path/to/save/image.jpg"
response = requests.get(url)
with open(file_path, "wb") as file:
file.write(response.content)
wget
:wget
库是一个用于下载文件的库,可以在Python中使用。import wget
url = "https://example.com/image.jpg"
file_path = "path/to/save/image.jpg"
wget.download(url, file_path)
这些方法都可以通过指定URL和文件路径来下载图片。请根据您的需求选择最适合的方法。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
相关推荐:python下载包的方法有哪些