Python requests库下载图片

313 阅读1分钟
import requests
import time
import os

def download_img():
    url = 'http://pic.58pic.com/58pic/16/42/96/56e58PICAu9_1024.jpg'
    print('开始下载图片')
    t1 = time.time()
    response = requests.get(url,stream = True)
    with open(os.path.abspath('build/demo.jpg'),'wb') as img:
        for tmp in response.iter_content(128):
            img.write(tmp)
    print('下载完成耗时:%s,文件路径:%s'%(time.time()-t1,os.path.abspath('build/demo.jpg')))