python下載圖片簡單教程

來源:互聯網
上載者:User

標籤:python下載圖片

通過urlretrieve方法
import os
#匯入os包,沒有該模組的可通過pip installl 命令安裝模組
from urllib.request import urlretrieve
#匯入urllib模組
IMAGE_URL="https://bpic.588ku.com/element_pic/00/16/10/21580951c08d0a9.jpg!/fw/208/quality/90/unsharp/true/compress/true"
#設定一個圖片的url,隨便一個即可
os.makedirs(‘./img/‘, exist_ok=True)
#通過os在當前的工作路徑下製作一個img目錄,可以不需要這句話
urlretrieve(IMAGE_URL, ‘./img/image1.png‘)
#通過urllib的urlretrieve方法使得把圖片下載儲存到指定的目錄
print(‘ok‘)
#確認是否執行成功
通過requests方法擷取
import os
import requests
#匯入os包,沒有該模組的可通過pip installl 命令安裝模組
from urllib.request import urlretrieve
#匯入urllib模組
IMAGE_URL = "https://bpic.588ku.com/element_pic/00/16/10/21580951c08d0a9.jpg!/fw/208/quality/90/unsharp/true/compress/true"
#設定一個圖片的url,隨便一個即可
r = requests.get(IMAGE_URL, stream=True)
with open(‘./img/image3.png‘, ‘wb‘) as f:
for chunk in r.iter_content(chunk_size=32):
f.write(chunk)
print(‘ok‘)

python下載圖片簡單教程

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.