爬取win4000全站点的高清壁纸

获取win4000全站点的高清壁纸,需要在同级目录下建立“allbz”文件夹

#!/usr/bin/env python
# -*- coding:utf-8 -*-
# Author: ss

from bs4 import BeautifulSoup
import requests
import time
import os


headers = {
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.186 Safari/537.36',
}


def get_image(url, title1, title2, title3, abc):
    #url = 'http://pic1.win4000.com/wallpaper/2018-05-10/5af43304a2837.jpg'
    data = requests.get(url=url, headers=headers)
    time.sleep(1)
    with open('.\\allbz\\' + title1 + '\\' + title2 + '\\' + title3 + str(abc) + '.jpg', 'wb+') as f:
        f.write(data.content)
        print("已下载{}张".format(abc), url, title3)


def get_jpg_url(url, title1, title2):
    #url = 'http://www.win4000.com/wallpaper_detail_145701.html'
    data = requests.get(url=url, headers=headers)
    soup = BeautifulSoup(data.text, 'lxml')
    time.sleep(2)
    url = soup.select('img.pic-large')[0].get('src')
    title3 = soup.select('img.pic-large')[0].get('title')
    global abc
    abc += 1
    get_image(url, title1, title2, title3, abc)


def get_one_url(url, title1, title2):
    #url ='http://www.win4000.com/wallpaper_detail_145701.html'
    data = requests.get(url=url, headers=headers)
    soup = BeautifulSoup(data.text, 'lxml')
    urls = soup.select('#scroll')
    for i in urls:
        data1 = i.select('li > a')
        for m in data1:
            url = m.get('href')
            get_jpg_url(url, title1, title2)


def get_list(url, title1):
    #url ='http://www.win4000.com/wallpaper_195_0_0_1.html'
    data = requests.get(url=url, headers=headers)
    soup = BeautifulSoup(data.text, 'lxml')
    urls = soup.select(
        'div.Left_bar > div.list_cont.Left_list_cont > div > div > div > ul')
    global abc
    for i in urls:
        data1 = i.select('li > a')
        for m in data1:
            url = m.get('href')
            title2 = m.select('img')[0].get('title')
            if os.path.exists('.\\allbz\\' + title1 + '\\' + title2):
                continue
            else:
                os.mkdir('.\\allbz\\' + title1 + '\\' + title2)
                print('开始下载{}'.format(title2))
                get_one_url(url, title1, title2)


def get_fenlei():
    url = 'http://www.win4000.com/wallpaper.html'
    data = requests.get(url=url, headers=headers)
    soup = BeautifulSoup(data.text, 'lxml')
    urls = soup.select('div.cont2')
    for i in urls:
        datas = i.select('a')
        for m in datas:
            url = m.get('href')
            title1 = m.text
            if title1 == '全部':
                continue
            else:
                if os.path.exists('.\\allbz\\' + title1):
                    continue
                else:
                    os.mkdir('.\\allbz\\' + title1)
                    print('开始下载{}'.format(title1))
                    get_list(url, title1)

abc = 0
get_fenlei()
效果图

LOL英雄联盟睡衣守护者原画图片壁纸

强烈推荐文章:
2020年4月至6月净利润断层实战小结-真香!

你可能感兴趣的:(爬取win4000全站点的高清壁纸)