正则爬取唯一图库海量美女图片(自动生成文件夹并储存)

# -*- coding: utf-8 -*-
__author__ = '木之易'
__date__ = '2018/8/7 20:17'

# from urllib.request import urlretrieve
from urllib import request
import re, os

class WeddingImageSpider(object):

    def __init__(self, t_id):
        # 拼接完整地址
        self.url = 'http://www.mmonly.cc'+t_id
        self.title = 'images'
        self.html = ''
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:61.0) Gecko/20100101 Firefox/61.0'}
        self.create_directry()
        self.count = 0

    def create_directry(self):
        """
        创建存放图片的文件夹
        :return:
        """
        # 获取源代码
        self.get_html(self.url)
        # 2.获取帖子标题
        pattern = re.compile(r'(.*?)', re.S)
        # print(pattern)
        rs = re.search(pattern, self.html)

        # 3.提取帖子标题,并赋值给self.title
        if rs:
            self.title = rs.group(1)

        # 4.判断文件/目录是否存在
        if not os.path.exists(self.title):
            # 没有self.title这个文件夹,创建这个文件夹
            # mkdir() 创建文件夹
            os.mkdir(self.title)

    def get_total(self):
        """获取下一页"""
        self.get_html(self.url)
        pattern = re.compile(r'
(.*?)
', re.S) res = re.search(pattern, self.html) # 若找到 if res: ul_html = res.group() # 找到含图片网址和关键字的标签 print(ul_html) links = re.findall(re.compile(r"

 

你可能感兴趣的:(python)