栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python 爬取图片 壁纸自动切换

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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文件




    F:/python/pythonFile/wallpaper/img

    60

运行桌面查看效果

**

3.打包成exe文件

**
1.win+R
cmd 打开命令行窗口
首先安装pyinstaller,使用安装命令:pip3 install pyinstaller
2.找到python文件夹运行cmd回车打开命令行

运行命令 pyinstaller -F 文件名.py
成功后 项目dist目录下有exe程序,把他移到.py文件同一目录

双击运行

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/342129.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号