Python 下载图片的三种方法【总结】

  1. import os  
  2. os.makedirs('./image/', exist_ok=True)  
  3. IMAGE_URL = "http://www.image.com/3.jpg"  
  4.   
  5. def urllib_download():  
  6.     from urllib.request import urlretrieve  
  7.     urlretrieve(IMAGE_URL, './image/3.jpg')       
  8.   
  9. def request_download():  
  10.     import requests  
  11.     r = requests.get(IMAGE_URL)  
  12.     with open('./image/img2.png''wb') as f:  
  13.         f.write(r.content)                        
  14.   
  15. def chunk_download():  
  16.     import requests  
  17.     r = requests.get(IMAGE_URL, stream=True)      
  18.     with open('./image/img3.png''wb') as f:  
  19.         for chunk in r.iter_content(chunk_size=32):  
  20.             f.write(chunk)  
  21.   
  22. urllib_download()  
  23. print('download img1')  
  24. request_download()  
  25. print('download img2')  
  26. chunk_download()  
  27. print('download img3')  

猜你喜欢

转载自blog.csdn.net/wangpengfei163/article/details/80235096
今日推荐