爬取zol壁纸网站上所有的壁纸

# _*_ coding:utf-8 _*_
"""爬取zol壁纸网站上所有的壁纸"""

import time
from bs4 import BeautifulSoup
import requests
class ZolSpider(object):
    def __init__(self, url=''):
        self.url = url
        self.base_url = 'http://desk.zol.com.cn'

    def get_html(self):
        """将获取到的html文本经过beautifulsoup加工"""
        url = self.url
        html = requests.get(url)
        soup_html = BeautifulSoup(html.content, 'html.parser')
        return soup_html

    def spider(self):
        """调用类别爬虫"""
        CategorySpider(self.base_url).category_spider()
# 类别
class CategorySpider(ZolSpider):
    def get_category_url(self):
        """获取所有的类别"""
        soup_html = self.get_html()
        category_url = []
        category = soup_html.find('dl', class_='filter-item first clearfix')
        for child in category.find_all('a'):
            category_url.append(self.base_url + child['href'])
        return category_url

    def category_spider(self):
        """调用图片列表爬虫"""
        for url in self.get_category_url():
            print('Current_Category:' + url.split('.cn')[-1])
            PhotoListSpider(url).list_spider()
            time.sleep(10)
# 图片列表
class PhotoListSpider(ZolSpider):
    def get_photo_list_url(self):
        """获取图片列表链接"""
        soup_html = self.get_html()
        photo_list_url = []
        photo_list = soup_html.find_all('li', class_='photo-list-padding')
        for child in photo_list:
            photo_list_url.append(self.base_url + child.a['href'])
        return photo_list_url

    def get_next_page_url(self):
        """获取下一页链接"""
        soup_html = self.get_html()
        next_page_tag = soup_html.find('a', id='pageNext')
        next_page_url = self.base_url + next_page_tag['href']
        return next_page_url

    def list_spider(self):
        """递归遍历所有列表"""
        print(u'Current_List:' + self.url.split('.cn')[-1])
        next_page = self.get_next_page_url()
        self.photo_spider()
        if next_page:
            PhotoListSpider(next_page).list_spider()
            time.sleep(5)

    def photo_spider(self):
        """调用图片爬虫"""
        for url in self.get_photo_list_url():
            print('Current_Set:' + url)
            PhotoSpider(url).photo_spider()
            time.sleep(2)
# 图片展示
class PhotoSpider(ZolSpider):
    def get_photo_url(self):
        """获取图片链接"""
        soup_html = self.get_html()
        img_tag = soup_html.find('img', id='bigImg')
        img_url = img_tag['src']
        return img_url

    def get_next_page_url(self):
        """获取下一页链接"""
        soup_html = self.get_html()
        next_page_tag = soup_html.find('div', id='photo-next')
        _next_page_url = self.base_url + next_page_tag.a['href']
        return _next_page_url

    def photo_spider(self):
        """递归遍历所有图片"""
        print('Photo_Url: ' + self.get_photo_url())
        next_page = self.get_next_page_url()
        if 'javascript' not in next_page:
            PhotoSpider(next_page).photo_spider()
if __name__ == '__main__':
    ZolSpider().spider()

你可能感兴趣的:(爬取zol壁纸网站上所有的壁纸)