原文链接: Python3 下载文件
Python 3 下载 文件
1import requests 2 3url = "https://static.oschina.net/uploads/space/2018/0124/212222_LkxI_1428332.jpg" 4file_name = url.split('/')[-1] 5 6 7def download1(): 8 with open(file_name, mode='wb+') as f: 9 f.write(requests.get(url).content) 10 11 12url = "http://f004.bai.com/data/uploads/2013/0315/10/1363314292740038.jpg" 13file_name = url.split('/')[-1] 14 15 16def download2(): 17 # 使用流下载大型文件 18 r = requests.get(url, stream=True) 19 with open(file_name, mode='wb+') as f: 20 for chunk in r.iter_content(chunk_size=32): 21 f.write(chunk) 22 23 24download2() 25 26url = "http://img1.3lian.com/img013/v3/74/d/41.jpg" 27file_name = url.split('/')[-1] 28from urllib.request import urlretrieve 29 30 31def download3(): 32 # 返回一个元组,文件路径和 ('41.jpg', <http.client.HTTPMessage object at 0x000001EC87FC8128>) 33 # Returns a tuple containing the path to the newly created 34 # data file as well as the resulting HTTPMessage object. 35 res = urlretrieve(url, file_name) 36 print(res) 37 38 39download3()