python下载图片的几种方法

一. urllib

# python2
import urllib url
= r"https://storage.googleapis.com/imgfave/image_cache/1491751695365459.jpg" path = r"D:\my.jpg"   #字符串前加r 防止被转义 data = urllib.urlretrieve(url,path) #py -3 #data = urllib.request.urlretrive(url,path)

 

#python3
import urllib.request 
url = r"https://storage.googleapis.com/imgfave/image_cache/1491751695365459.jpg"  
path = r"D:\my.jpg"    #字符串前加r 防止被转义
data = urllib.request.urlretrieve(url,path)

 

二. requests

import requests
url = 'https://storage.googleapis.com/imgfave/image_cache/1491751695365459.jpg'
with open(r'D:\sss.jpg', 'wb') as handle:
    response = requests.get(url, stream=True)
    for block in response.iter_content(1024):
        if not block:
            break
        handle.write(block)
    print('done')

 

posted @ 2017-04-12 16:53  Garvey  阅读(574)  评论(0编辑  收藏  举报