python 下載 JPG 圖片

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)



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章