码迷,mamicode.com
首页 > 其他好文 > 详细

爬取美图(参考其他博客)

时间:2017-11-19 13:33:53      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:urllib   text   ade   join   sel   main   ons   bs4   wow   

import requests
from bs4 import BeautifulSoup
import os
import urllib
import random


class mzitu():
    def all_url(self, url):
        html = self.request(url)
        all_a = BeautifulSoup(html.text, lxml).find(div, class_=all).find_all(a)
        for a in all_a:
            title = a.get_text()
            print(u开始保存:, title)
            path = str(title).replace("?", _)
            if not self.mkdir(path):  ##跳过已存在的文件夹
                print(u已经跳过:, title)
                continue
            href = a[href]
            self.html(href)

    def html(self, href):
        html = self.request(href)
        max_span = BeautifulSoup(html.text, lxml).find(div, class_=pagenavi).find_all(span)[-2].get_text()
        for page in range(1, int(max_span) + 1):
            page_url = href + / + str(page)
            self.img(page_url)

    def img(self, page_url):
        img_html = self.request(page_url)
        img_url = BeautifulSoup(img_html.text, lxml).find(div, class_=main-image).find(img)[src]
        self.save(img_url, page_url)

    def save(self, img_url, page_url):
        name = img_url[-9:-4]
        try:
            img = self.requestpic(img_url, page_url)
            f = open(name + .jpg, ab)
            f.write(img.content)
            f.close()
        except FileNotFoundError:  ##捕获异常,继续往下走
            print(u图片不存在已跳过:, img_url)
            return False

    def mkdir(self, path):  ##这个函数创建文件夹
        path = path.strip()
        isExists = os.path.exists(os.path.join("C:\d\mzitu", path))
        if not isExists:
            print(u建了一个名字叫做, path, u的文件夹!)
            os.makedirs(os.path.join("C:\d\mzitu", path))
            os.chdir(os.path.join("C:\d\mzitu", path))  ##切换到目录
            return True
        else:
            print(u名字叫做, path, u的文件夹已经存在了!)
            return False

    def requestpic(self, url, Referer):  ##这个函数获取网页的response 然后返回
        user_agent_list = [             "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"             "Mozilla/5.0 (X11; CrOS i686 2268.111.0) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.57 Safari/536.11",             "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1092.0 Safari/536.6",             "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",             "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/19.77.34.5 Safari/537.1",             "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.9 Safari/536.5",             "Mozilla/5.0 (Windows NT 6.0) AppleWebKit/536.5 (KHTML, like Gecko) Chrome/19.0.1084.36 Safari/536.5",             "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",             "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",             "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_0) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1063.0 Safari/536.3",             "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",             "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1062.0 Safari/536.3",             "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",             "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",             "Mozilla/5.0 (Windows NT 6.1) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.1 Safari/536.3",             "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.3 (KHTML, like Gecko) Chrome/19.0.1061.0 Safari/536.3",             "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24",             "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/535.24 (KHTML, like Gecko) Chrome/19.0.1055.1 Safari/535.24"
        ]
        ua = random.choice(user_agent_list)
        headers = {User-Agent: ua, "Referer": Referer}  ##较之前版本获取图片关键参数在这里
        content = requests.get(url, headers=headers)
        return content

    def request(self, url):  ##这个函数获取网页的response 然后返回
        headers = {
            User-Agent: "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/22.0.1207.1 Safari/537.1"}
        content = requests.get(url, headers=headers)
        return content


Mzitu = mzitu()  ##实例化
Mzitu.all_url(http://www.mzitu.com/all)  ##给函数all_url传入参数  你可以当作启动爬虫(就是入口)
print(u恭喜您下载完成啦!)

来源:http://www.cnblogs.com/wangbg/p/7282543.html

爬取美图(参考其他博客)

标签:urllib   text   ade   join   sel   main   ons   bs4   wow   

原文地址:http://www.cnblogs.com/realmonkeykingsun/p/7859403.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!