吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1893|回复: 44
上一主题 下一主题
收起左侧

[Python 原创] 一个壁纸爬虫脚本

[复制链接]
跳转到指定楼层
楼主
bobo2017365 发表于 2023-12-30 22:30 回帖奖励
本帖最后由 bobo2017365 于 2023-12-30 22:31 编辑

闲来无事,想找一些图片当壁纸,但一张一张下又太慢,能用程序完成的,坚决不能手动。
PS:白嫖,但咱也要尊重网站的维护者。

[Python] 纯文本查看 复制代码
 import requests
from bs4 import BeautifulSoup
import time

my_dict = {
    "1": "4Kxinnian",
    "2": "4Kdujia",
    "3": "4kyouxi",
    "4": "4kdongman",
    "5": "4kmeinv",
    "6": "4kfengjing",
    "7": "4kyingshi",
    "8": "4kqiche",
    "9": "4kdongwu",
    "10": "4kbeijing",
    "11": "pingban",
    "12": "shoujibizhi",
}


def get_spec_pages(bizhi_type):
    page_total = 1
    for i in range(page_total):
        get_single_page(i, bizhi_type)


def get_single_page(page, bizhi_type=""):
    try:
        page = int(page)
    except ValueError:
        return False
    while True:
        if page == 0:
            pic_list_url = 'https://pic.netbian.com/{}/index.html'.format(bizhi_type)
        else:
            pic_list_url = 'https://pic.netbian.com/{}/index_{}.html'.format(bizhi_type, page)
        Myheaders = {
            'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_4) AppleWebKit/537.36 '
                        '(KHTML, like Gecko) Chrome/52.0.2743.116 Safari/537.36'
        }
        #req = requests.session()
        pic_list_html = requests.get(pic_list_url, headers = Myheaders)
        pic_list_html.encoding = 'gbk'
        soup = BeautifulSoup(pic_list_html.text,'lxml')
        pic_lists = soup.find('ul',{'class' : 'clearfix'}).find_all('li')

        x = 1
        for li in pic_lists:
            pic_url = 'https://pic.netbian.com/' + li.a.get('href')

            pic_html = requests.get(pic_url,headers = Myheaders)
            pic_html.encoding = 'gbk'

            sp = BeautifulSoup(pic_html.text,'lxml')

            pic_download = 'https://pic.netbian.com/' + sp.find('a',{'id' : 'img'}).img.get('src')
            #获取返回的字节类型
            img = requests.get(pic_download, headers=Myheaders).content
            path = str(sp.find('a',{'id' : 'img'}).img.get('title')) + ".jpg"

            with open(path, 'wb') as f:
                    f.write(img)
                    time.sleep(1)
                    print("第【{}】页第【{}】张图片下载完成!".format(page+1,x))
                    x += 1

        page += 1
        if page == 15:
            print('下载结束!')
            break

def main():
    # 打印带序号的字符串供用户选择
    for i, (key, value) in enumerate(my_dict.items(), start=1):
        print(f"{key}: {value}")

    # 等待用户输入序号
    choice = input("请输入要下载的序号:")

    # 根据用户输入的序号查找字典中的值并拼接成字符串
    if choice.isdigit():
        index = int(choice)
        selected_key = list(my_dict.keys())[index - 1]  # 索引从0开始,所以要减1
        selected_value = my_dict[selected_key]
        result = f"{selected_key}: {selected_value}"
        get_spec_pages(selected_value)
    else:
        print("无效的序号,请输入一个正整数。")

if __name__ == '__main__':
    main()

免费评分

参与人数 3吾爱币 +9 热心值 +3 收起 理由
苏紫方璇 + 7 + 1 欢迎分析讨论交流,吾爱破解论坛有你更精彩!
soughing + 1 + 1 我很赞同!
StoreDS + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

推荐
 楼主| bobo2017365 发表于 2023-12-30 22:49 |楼主
StoreDS 发表于 2023-12-30 22:37
最近在学习中,感谢楼主分享

一起加油
推荐
StoreDS 发表于 2023-12-30 22:37
头像被屏蔽
推荐
xiaoxiaoyiyu 发表于 2024-1-19 15:52
4#
tybo8083006 发表于 2023-12-30 23:21
感谢楼主分享,学习一下
5#
lxbinternet 发表于 2023-12-30 23:23
只能下载20张,然后就报错。
6#
bzgl666 发表于 2023-12-30 23:45
感谢分享,学习
7#
xiezx2023 发表于 2023-12-30 23:57
不错的选择哦,感谢楼主
8#
wsl1019 发表于 2023-12-31 00:03
学习,感谢分享
9#
baliao 发表于 2023-12-31 00:10
感谢分享! 不过这个下载下来的不是4k, 尺寸和文件大小也不一样.
10#
Asy_少洋 发表于 2023-12-31 02:24
可惜不是原图;、
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-4-28 10:32

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表