2、使用Python3爬取美女图片-网站中的妹子自拍一栏

代码还有待优化,不过目的已经达到了

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
复制代码

执行效果:

 

有时间的话会把代码进行优化处理的。。。

 

posted @   哈喽哈喽111111  阅读(1133)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
阅读排行:
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· Docker 太简单,K8s 太复杂?w7panel 让容器管理更轻松!
点击右上角即可分享
微信分享提示