Python 下载图片的方法

xiaoxiao2025-11-09  6

import os os.makedirs('./image/', exist_ok=True) IMAGE_URL = "http://image.nationalgeographic.com.cn/2017/1122/20171122113404332.jpg"   def urllib_download():     from urllib.request import urlretrieve     urlretrieve(IMAGE_URL, './image/img1.png')        def request_download():     import requests     r = requests.get(IMAGE_URL)     with open('./image/img2.png', 'wb') as f:         f.write(r.content)                         def chunk_download():     import requests     r = requests.get(IMAGE_URL, stream=True)         with open('./image/img3.png', 'wb') as f:         for chunk in r.iter_content(chunk_size=32):             f.write(chunk)   urllib_download() print('download img1') request_download() print('download img2') chunk_download() print('download img3')  

 

import requests import os from hashlib import md5 def getimage(): url='http://pic19.nipic.com/20120308/4970979_102637717125_2.jpg' request=requests.get(url) # 路径 文件名 后缀 file_name = '{0}/{1}.{2}'.format(os.path.dirname('G:\pic\\'), 'shizi', 'jpg') f=open(file_name,'wb') f.write(request.content) f.close() if __name__ == '__main__': getimage()
转载请注明原文地址: https://www.6miu.com/read-5039357.html

最新回复(0)