zoukankan      html  css  js  c++  java
  • 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

    执行效果:

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

  • 相关阅读:
    Nhibernate1
    控制反转(IoC)
    Windbg是windows平台上强大的调试器
    Java 7 语法新特性
    区间数据计算
    红黑树数据结构剖析
    .net下灰度模式图像
    如何配置Hyper-V的虚拟机通过主机网络上网 (NAT)
    产品落地
    poj-3898 Software Industry Revolution DP
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/8299358.html
Copyright © 2011-2022 走看看