代码还有待优化,不过目的已经达到了
1、先执行如下代码:
1 #!/usr/bin/env python 2 #-*- coding: utf-8 -*- 3 4 import urllib 5 import requests 6 import random 7 from bs4 import BeautifulSoup 8 import json 9 10 # 获取图片路径并保存 11 image_list = [] 12 for num in range(1,331): 13 url = "http://www.mzitu.com/zipai/comment-page-" + str(num) + "/#comments" 14 response = requests.get(url) 15 response.encoding = response.apparent_encoding 16 soup = BeautifulSoup(response.text, features='html.parser') 17 target = soup.find(id="comments") 18 li_list = target.find_all('li') 19 20 for i in li_list: 21 a = i.find('img') 22 if a: 23 image = a.attrs.get('src') 24 image_list.append(image) 25 print('图片路径为%s' % len(image_list)) 26 27 with open('image.txt','w') as obj: 28 data = json.dumps(image_list) 29 obj.write(data)
2、在执行如下代码:
1 # 下载图片并保存 2 with open('image.txt','r') as f_obj: 3 data = json.load(f_obj) 4 count = 1 5 for url in data: 6 save_path = "D:\\zipai\\" 7 urllib.request.urlretrieve(url, save_path + str(count) + '.jpg') 8 count += 1
执行效果:
有时间的话会把代码进行优化处理的。。。
转载于:https://www.cnblogs.com/sanduzxcvbnm/p/8299358.html
神龙|纯净稳定代理IP免费测试>>>>>>>>天启|企业级代理IP免费测试>>>>>>>>IPIPGO|全球住宅代理IP免费测试