利用Python爬取美女网站图片

最近在学习Python爬虫,拿了个美女网站做了实验。

脚本源码如下:(图片会存在相对应的图集编号的文件夹下)

#!/usr/bin/env python

# -*- coding: utf-8 -*-

# @Date : 2017-05-12

# @Author : yangfannie

# @name : PicDownload.py

# @Link : http://yangfannie.com

#coding:utf-8

import requests

import re

import os

from bs4 import BeautifulSoup

import urllib

url = "http://www.mm131.com/xinggan/"   ##设置python爬取的网站链接

content = urllib.urlopen(url).read()    ##读取网站页面信息

res_tr = r'<dl class="list-left public-box">(.*?)</dl>'   ##使用正则筛出需要内容区间

m_tr = re.findall(res_tr,content,re.S|re.M)

for line in m_tr:

    link_list = re.findall(r"(?<=href=\"http://www.mm131.com/xinggan/).+?(?=\.html)", line)    ##使用正则筛出图集编号

    for pic_id in link_list:

    os.chdir("/opt/pic")   ##设置图片保存目录

    homedir = os.getcwd()

    fulldir = unicode(os.path.join(homedir,pic_id),encoding='utf-8') ##图片保存在指定目录,并根据套图id设置目录

    print fulldir

    if not os.path.isdir(fulldir):

           os.makedirs(fulldir)
    ##通过图集编号获取每个图集下面有多少张图片
    url='http://www.mm131.com/xinggan/%s.html' % pic_id
    html = requests.get(url).text
    soup = BeautifulSoup(html, 'html.parser') #使用soup取关键字,上一行会报错UserWarning: No parser was explicitly specified
    ye = soup.span.string
    ye_count = re.search('\d+',ye)
    print('pages:共%d页' % int(ye_count.group()))
    def downpic(pic_id):
        n = 1
        url='http://www.mm131.com/xinggan/%s.html' % pic_id
        while n <= int(ye_count.group()): #翻完停止
        #下载图片
        try:
            if not n == 1:
            url='http://www.mm131.com/xinggan/%s_%s.html' % (pic_id,n) #url随着n的值变化的
            html = requests.get(url).text
            pic_url = re.search(r'img alt=.* src="(.*?)" /',html,re.S) #使用正则去关键字
            pic_s = pic_url.group(1)
            print(pic_s)
            pic= requests.get(pic_s, timeout=2)
            pic_cun = fulldir + '/' + pic_id + '_' + str(n) + '.jpg'  ##下载图片命名
            fp = open(pic_cun,'wb')
            fp.write(pic.content)
            fp.close()
            n += 1
        except requests.exceptions.ConnectionError:
            print("【错误】当前图片无法下载")
            continue
    if __name__ == '__main__':
        downpic(pic_id)
weinxin
聂扬帆博客
一个分享IT运维相关工作经验和实战技巧的个人博客

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: