python 爬取图片 壁纸开机自启自动切换

1.爬取图片,作为壁纸,用来接下来的壁纸自动切换
python代码里的库如果报错,记得下库
pip install 加库名
在这里插入图片描述

代码

import requests
import parsel

# 10,15爬取10到15页的图片
for pag in range(10, 15):
    print(f'=========={pag}页=====')
    # 设置网页
    url = f'http://www.netbian.com/meinv/index_{pag}.htm'
    # 设置浏览器
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'}
    #
    responrse = requests.get(url=url, headers=headers)
    # html_data = responrse.content.decode('utf-8')
    responrse.encoding = responrse.apparent_encoding
    # print(responrse.text)
    selctor = parsel.Selector(responrse.text)
    lis = selctor.css('.list li')
    # print(lis)
    for li in lis:
        title = li.css('b::text').get()
        # print(title)
        if title:
            href = 'http://www.netbian.com/' + li.css('a::attr(href)').get()
            # print(href, title)
            response_i = requests.get(url=href, headers=headers)
            seletor_i = parsel.Selector(response_i.text)
            img_url = seletor_i.css('.pic img::attr(src)').get()
            # print(href,title,img_url)
            img_countent = requests.get(url=img_url, headers=headers).content
            # 图片保存在当前img目录下
            with open('img\\' + title + '.jpg', mode='wb') as f:
                f.write(img_countent)
                print('正在保存:' + title)


设置浏览器
在这里插入图片描述
运行代码,图片保存在img目录下

2.编写自动切换壁纸代码

import random
import ctypes
import time
import datetime
import os
from xml.dom.minidom import parse

picture_list = []
path = ""
random_time = 68


def read_xml():
    doc = parse("./config.xml")
    root = doc.documentElement
    global path,random_time
    path = root.getElementsByTagName("path")[0].firstChild.data
    random_time = int(root.getElementsByTagName("random_time")[0].firstChild.data)

def list_all_pic(pic_dir):
    pic_files = []
    files_list  =os.listdir(pic_dir)
    for i in range(0,len(files_list)):
        full_path = os.path.join(pic_dir,files_list[i])
        if os.path.isfile(full_path):
            if full_path.rsplit(".")[-1].lower() in ["jpg","bmp","jpeg","png"]:
                pic_files.append(full_path)
    return pic_files
def random_pic_index():
    return picture_list[random.randint(0,len(picture_list)-1)]

def main():
    read_xml()
    global picture_list
    picture_list = list_all_pic(path)
    if len(picture_list) == 0:
        print("文件夹中没有壁纸")
        exit(1)
    while True:
        file_name = random_pic_index()
        ctypes.windll.user32.SystemParametersInfoW(20,0,file_name,0)
        print("{} 图像名:{}".format(datetime.datetime.strftime(datetime.datetime.now(),"%y-%m-%d %H:%M:%S"),file_name))
        time.sleep(random_time)

if __name__ == '__main__':
    main()




```python

编写.xml文件

<?xml version="1.0"?>
<config>
<!--    保存图片目录-->
    <path>F:/python/pythonFile/wallpaper/img</path>
<!--    图片切换时间-->
    <random_time>60</random_time>
</config>

运行桌面查看效果

**

3.打包成exe文件

**
1.win+R
cmd 打开命令行窗口
首先安装pyinstaller,使用安装命令:pip3 install pyinstaller
2.找到python文件夹运行cmd回车打开命令行
在这里插入图片描述
运行命令 pyinstaller -F 文件名.py
成功后 项目dist目录下有exe程序,把他移到.py文件同一目录
在这里插入图片描述
双击运行

3.添加到系统自启文件夹,实现开机自启

1.创建快捷方式
2.命令行打开自启文件夹(shell:startup),放入快捷方式
在这里插入图片描述

壁纸实现开机自启自动切换

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

浏贻笑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值