使用python搜集壁纸

使用python搜集壁纸

刚好最近在研究学习python,由于兴趣爱好,自己整了一个小小的壁纸收集器!

  • 此软件的壁纸均来自于全球第一的壁纸网站wallhaven;

  • 壁纸搜集目前只有wallhaven 中的toplist 壁纸(排行榜中的壁纸);

  • 在wallpaper.exe文件路径下,创建一个wallpaper的文件夹;

  • 软件是免安装版的,解压、双击运行wallpaper.exe 即可;

1、python代码:

'''
python 壁纸搜集
'''
#导入相关包
import requests
from bs4 import BeautifulSoup
import time
import socket
socket.setdefaulttimeout(500)

#定义几个函数,方便调用
def main():
    while True:
        menu()
        try:
            choice=int(input('请选择:'))
        except:
            print('输入错误,请重新输入!')
            continue
        if choice in range(2):
            if choice == 0:
                answer=input('你确定要退出 wallpaper 搜集系统吗?y/n\t')
                if answer=='y' or answer=='Y':
                    print('感谢使用!!!')
                    break   #退出系统
                else:
                    continue
            elif choice == 1:
                wallpaper()   #图片搜集
        else:
            print('提示:请重新输入!!!')

def menu():
    print("                    欢迎使用 wallpaper 搜集系统                      ")
    print('---------------------------1、下载图片------------------------------')
    print('---------------------------0、退出系统------------------------------')

def wallpaper():
    num = int(input('请输入您想获取第几页图片(一页20张):'))
    for i in range(num,num+1):
        url = f"https://wallhaven.cc/toplist?page={i}"
        headers={
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36"
        }

        resp = requests.get(url,headers=headers)
        # print(resp.text)
        main_page = BeautifulSoup(resp.text,"html.parser")
        alist = main_page.find("div",class_="thumbs-container thumb-listing infinite-scroll").find_all("a",class_="preview")
        # print(alist)
        for a in alist:
            href = a.get("href")
            # 拿到子页面代码
            headers = {
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36"
            }
            sub_page_resp = requests.get(href,headers=headers)
            sub_page_text = sub_page_resp.text
            sub_page = BeautifulSoup(sub_page_text,"html.parser")
            div = sub_page.find("div",class_="scrollbox")
            img = div.find("img")
            src = img.get("src")
            # print(src)
            #下载图片
            img_resp = requests.get(src)
            # img_resp.content    #这里拿到的是字节
            img_name = src.split("/")[-1]   #拿到url 中的最后一个 / 以后的的内容
            with open(f"wallpaper/"+img_name,mode="wb") as wfile:
                wfile.write(img_resp.content)    #图片内容写入文件,相当于下载图片了

            print("over!!!",img_name)
            time.sleep(1)
        print("---------------------------------------")
    print("over!!!")

if __name__ == '__main__':
    main()

2、打包代码,构建成 .exe的执行文件:

#打包代码,下面的路径填自己代码文件所在的路径
pyinstaller -F D:\study\python\pycode\test\wallpaper.py

#打包好后,可以在代码目录的dist文件中看到一个 .exe结尾的文件
wallpaper.exe
#接下来双击运行即可
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值