如何下载图片新闻并将其写入文件

以http://mhi.com.my/paper/2016/Julai/290716/290716.pdf网站为例:

 1 import requests
 2 response=requests.get('http://mhi.com.my/paper/2016/Julai/290716/290716.pdf')
 3 #用requests.get的方法获得网站响应
 4 
 5 
 6 while True:
 7     try:
 8         response = requests.get('http://mhi.com.my/paper/2016/Julai/290716/290716.pdf')
 9         if '404 Not Found' in response.content:#response.content获取的是网站的二进制文件的内容
10         break#若该网站无图片新闻则跳出死循环 
11 
12      else: 
13         with open(day+mon+str(year).replace('20','')+'.pdf','w') as f : 
14           f.write(response.content) 
15           break

  

原文地址:https://www.cnblogs.com/flippedkiki/p/5722859.html