共計 651 個字符,預計需要花費 2 分鐘才能閱讀完成。
在 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 和文件路徑來下載圖片。請根據您的需求選擇最適合的方法。
丸趣 TV 網 – 提供最優質的資源集合!
正文完