共計(jì) 499 個(gè)字符,預(yù)計(jì)需要花費(fèi) 2 分鐘才能閱讀完成。
要使用 Python 下載照片,你可以使用以下步驟:
-
導(dǎo)入所需的模塊:
import requests
-
指定要下載的圖片的 URL:
image_url = "https://example.com/image.jpg"
-
發(fā)送 GET 請求以獲取圖片的二進(jìn)制數(shù)據(jù):
response = requests.get(image_url)
-
將響應(yīng)的內(nèi)容寫入文件:
with open("image.jpg", "wb") as file: file.write(response.content)
完整代碼如下所示:
import requests
image_url = "https://example.com/image.jpg"
response = requests.get(image_url)
with open("image.jpg", "wb") as file:
file.write(response.content)
在這個(gè)例子中,我們使用了 requests 模塊來發(fā)送 GET 請求并獲取圖片的二進(jìn)制數(shù)據(jù)。然后,我們使用 open
函數(shù)以二進(jìn)制寫入模式打開一個(gè)文件,并將響應(yīng)的內(nèi)容寫入文件中。你可以根據(jù)自己的需要更改文件名和路徑。
丸趣 TV 網(wǎng) – 提供最優(yōu)質(zhì)的資源集合!
正文完