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

执行效果:

 

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

 

转载于:https://www.cnblogs.com/sanduzxcvbnm/p/8299358.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值