import re import requests import json import os from urllib import request url = 'http://www.toutiao.com/a6589854444442092040/' headers ={ 'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.62 Safari/537.36', } # 新建文件夹 if not os.path.exists('download') : os.mkdir('download') response = requests.get(url,headers=headers) res_html = response.text # print(res_html) res_zhengze = r'gallery: JSON\.parse\((.*)\),' pattern=re.search(res_zhengze,res_html) res_1=pattern.group(1) res_2=json.loads(res_1) res_3 = json.loads(res_2) for res_4 in res_3['sub_images']: res_5=res_4['url'] filename = 'download/' + res_5.split('/')[-1] + '.jpg' # 下载图片 request.urlretrieve(res_5, filename) #print(res_5) #print(res_html) # with open('jr.html','wb')as f: # f.write(response.content)
获取头条图片并加载到本地(简易版)
猜你喜欢
转载自blog.csdn.net/weixin_42958164/article/details/81747165
今日推荐
周排行