Python2 的 urllib 與 urllib2 下載圖片後,顯示無法正常打開。原因可能爲,默認 post 方式下載,而圖片應該爲 get 方式下載。使用如下代碼,問題解決。
requests安裝: pip install requests
import requests
# 下載圖片 def dowloadPic(imageUrl,filePath): r = requests.get(imageUrl) with open(filePath, "wb") as code: code.write(r.content)
url = 'http://test.com/images/1803_android.jpg' filePath = "E://pics/1803.jpg" dowloadPic(url,filePath)