Python下载照片的三种方法

直接甩代码

import os
os.makedirs('./picture/', exist_ok=True) #这里在根目录创建一个“picture”文件夹 
URL = "https://imt-img.oss-cn-shenzhen.aliyuncs.com/test/青苹果/青苹_20200318210608_0.jpg"

#方法一
def urllib_download():
    from urllib.request import urlretrieve
    urlretrieve(URL, './picture/1.png')     
#方法二
def request_download():
    import requests
    r = requests.get(URL)
    with open('./picture/2.png', 'wb') as f:
        f.write(r.content)                      
#方法三
def chunk_download():
    import requests
    r = requests.get(URL, stream=True)    
    with open('./picture/3.png', 'wb') as f:
        for chunk in r.iter_content(chunk_size=32):
            f.write(chunk)

urllib_download()
print('download 1')
request_download()
print('download 2')
chunk_download()
print('download 3')

你可能感兴趣的:(Python下载照片的三种方法)