參考資料:陳會安,看圖學Python+Excel辦公室自動化程式設計,全華
1.下載單一圖檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | import requests from PIL import Image url = "https://csie.nfu.edu.tw/images/虎科大-資工系-t-w-LOGO-x.png" path = "LOGO.png" response = requests.get(url) if response.status_code == 200: with open(path, 'wb') as fp: for chunk in response: fp.write(chunk) print("LOGO圖檔已經下載") im = Image.open(path) im.show() else: print("錯誤! HTTP請求失敗...") |
執行結果:
LOGO圖檔已經下載
2.下載兩個png圖檔
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | import requests from PIL import Image url = ["https://csie.nfu.edu.tw/images/虎科大-資工系-t-w-LOGO-x.png", "http://nfuee.nfu.edu.tw/wp-content/uploads/2022/12/nfu-logo.png"] path = "LOGO" for i, pic in enumerate(url): response = requests.get(pic) if response.status_code == 200: with open(path+str(i+1)+".png", 'wb') as fp: for chunk in response: fp.write(chunk) print("LOGO", i+1,"圖檔已經下載") im = Image.open(path+str(i+1)+".png") im.show() else: print("錯誤! HTTP請求失敗...") |
執行結果:
LOGO 1 圖檔已經下載
LOGO 2 圖檔已經下載
沒有留言:
張貼留言