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

    执行效果:

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

  • 相关阅读:
    进程、线程、协程
    C++内存模型
    动态库dll与静态库lib
    virtual 虚函数表
    C++面试随笔
    alloc()、malloc()、calloc()、realloc()区别及用法
    C/C++ 面试题记录
    VC底层钩子程序在Win7/Vista下无效
    JMeter安装之后修成中文版
    明天开始 新的旅程
  • 原文地址:https://www.cnblogs.com/sanduzxcvbnm/p/8299358.html
Copyright © 2011-2022 走看看