欢迎来到好人卡资源网,专注网络技术资源收集,我们不仅是网络资源的搬运工,也生产原创资源。寻找资源请留言或关注公众号:烈日下的男人

【原创】python爬虫爬取壁纸,源码和 exe 文件

hacker sky995 2年前 (2022-02-03) 679次浏览 0个评论

本文及资源最后更新时间 2022-02-03 by sky995

运行截图

【原创】python爬虫爬取壁纸,源码和 exe 文件

【原创】python爬虫爬取壁纸,源码和 exe 文件

【原创】python爬虫爬取壁纸,源码和 exe 文件

注意:

  • 默认保存位置是 d:photo_from_52pojie.com

对于 exe 文件:

  • 不论是程序运行出错,还是成功下载,最后都会突然关闭页面。除非你眼光犀利到可以在那不到一秒的时间看见最后出来的字,否则想知道结果只能去 d:photo_from_52pojie.com 这个目录看看有没有下载下来
  • 软件爬取的是这个网站:http://www.netbian.com。如果要预览的话就去网站上看,不过,没法针对性的下载,但是网站上本来就可以免费下载,这个只是能够批量下载
  • 软件名随便起的

下载链接:https://wwe.lanzoui.com/isexEvewjne

代码

import requests
from lxml import etree
import chardet
import ua
import os

def select_catgory():
    print('''网站的壁纸按内容分类,共有 23 种,分别是:\n
    1. 日历    2. 动漫    3. 风景    4. 美女    5. 游戏    6. 影视    7. 动态\n
    8. 唯美    9. 设计    10.花卉    11.动物    12.节日    13.人物    14.美食\n
    15.水果    16.建筑    17.体育    18.军事    19.非主流  20.护眼    21.LOL\n
    22.王者荣耀 23.其他\n\n''')
    choice = int(input('按照分类下载,输入对应分类序号;随机下载,输入“0”。请输入:\n'))
    choices = ['http://www.netbian.com/',
                'http://www.netbian.com/rili/',
                'http://www.netbian.com/dongman/',
                'http://www.netbian.com/fengjing/',
                'http://www.netbian.com/meinv/',
                'http://www.netbian.com/youxi/',
                'http://www.netbian.com/yingshi/',
                'http://www.netbian.com/dongtai/',
                'http://www.netbian.com/weimei/',
                'http://www.netbian.com/sheji/',
                'http://www.netbian.com/huahui/',
                'http://www.netbian.com/dongwu/',
                'http://www.netbian.com/jieri/',
                'http://www.netbian.com/renwu/',
                'http://www.netbian.com/meishi/',
                'http://www.netbian.com/shuiguo/',
                'http://www.netbian.com/jianzhu/',
                'http://www.netbian.com/tiyu/',
                'http://www.netbian.com/junshi/',
                'http://www.netbian.com/feizhuliu/',
                'http://www.netbian.com/s/huyan/',
                'http://www.netbian.com/s/lol/',
                'http://www.netbian.com/s/wangzherongyao/',
                'http://www.netbian.com/qita/']
    global index_urls
    index_urls = []
    index_urls.append(choices[choice])
    global n
    n = int(input("需要下载多少张壁纸?\n"))

def get_code_type():
    # 请求首页
    global r
    r = requests.get('http://www.netbian.com/', headers=ua.ua())
    code_type = chardet.detect(r.content)['encoding']
    if code_type == 'GB2312':
        code_type = 'GBK'
    r.encoding = code_type

def get_pre(n):
    global page_urls
    page_urls = []
    # 网站总共 1216 页
    for i in range(1, n//20+2):
        # 获取每一张图片所在页面的 URL,来下载图片原图
        r_i = requests.get(index_urls[i-1], headers=ua.ua())
        html = etree.HTML(r_i.text)
        page_urls += html.xpath('body//*[@class="list"]/ul//li/a/@href')
        index_url = '{}index_{}.htm'.format(index_urls[0], i+1)
        index_urls.append(index_url)        

def get_jpg():
# 获取每一张图片的 URL,下载图片原图
    for page_url in page_urls:
        if page_urls.index(page_url) > n-1:
            break
        r_page = requests.get('http://www.netbian.com'+page_url, headers=ua.ua())
        if r.encoding == 'GBK':
            r_page.encoding = 'GBK'
        html = etree.HTML(r_page.text)
        jpg_url = html.xpath('body//*[@class="pic"]/p/a/img/@src')
        jpg_title = html.xpath('body//*[@class="pic"]/p/a/img/@title')
        jpg = list(zip(jpg_title, jpg_url))
        r_jpg = requests.get(jpg[0][1], headers=ua.ua())
        if r.encoding == 'GBK':
            r_jpg.encoding = 'GBK'
        if os.path.exists(r'd:\photo_from_52pojie.com'):
            pass
        else:
            os.mkdir(r'd:\photo_from_52pojie.com')
        with open(fr'd:\photo_from_52pojie.com\{jpg[0][0]}.jpg', 'wb') as f:
            f.write(r_jpg.content)
            print('第{}张图片下载完成!'.format(page_urls.index(page_url)+1))  

def main():
    get_code_type()
    try:
        select_catgory()
        get_pre(n)
        get_jpg()
        print(f'{n}张图片全部下载完成,默认保存位置是 d:\photo_from_52pojie.com')
    except ValueError:
        print('输入有误,请输入一个非负整数!')
    except IndexError:
        print('请输入正确的序号!')
    except:
        print('发生未知错误!')

main()

0


好人卡资源网 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:【原创】python爬虫爬取壁纸,源码和 exe 文件
喜欢 (1)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址