import requests,re,json,os
from urllib import request
创建文件夹
if not os.path.exists(‘ttss’):
os.mkdir(‘ttss’)
定义路由
url=’https://www.toutiao.com/a6581764599236788740/’
定义hear
headers={
‘User-agent’:’Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/68.0.3440.106 Safari/537.36’
}
response=requests.get(url,headers=headers)
html_text=response.text
print(html_content)
with open(‘jinritoutiao.html’,’wb’) as ff:
ff.write(html_content)
partten=’gallery: JSON.parse((.*)),’
res_object=re.search(partten,html_text)
if res_object:
#这是匹配到的具体内容
res_str1=res_object.group(1)
#转化两次才能成为字符串
res2=json.loads(res_str1)
res_dict=json.loads(res2)
# #打印确认是字典
# print(type(res_dict))
print(res_dict)
#取出所需路由
res_url=res_dict[‘sub_images’]
print(res_url)
#遍历取出所有url
for image_url in res_url:
pic_url=image_url[‘url’]
# print(pic_url)
#给图片命名
image_name=’ttss/’+ pic_url.split(‘/’)[-1]+’.jpg’
#保存图片到文件夹jinritoutiao
request.urlretrieve(pic_url,image_name)
else:
print(‘图片下载失败了’)