python实现桌面壁纸切换功能

作者:__师寇__ 时间:2023-05-26 04:38:49 

本文实例为大家分享了python实现桌面壁纸切换功能的具体实现方法,供大家参考,具体内容如下

大体分为两个部分

一、利用爬虫爬取壁纸

第一部分爬取图片url地址并且下载至本地
爬虫针对 http://image.so.com/ 【360壁纸写的】,如果要更换url地址自己改改


import requests
import json
import random
import os
#存放Ajax图片地址数据
img_url_dict={}
#创建图片tmp文件夹
if not os.path.exists('image'):
 os.mkdir('image')
#爬取图片url地址
def getImgurl(root_url,sn):
 params={
   'ch': 'wallpaper',
   't1': 157,
   'sn': sn,
   'listtype': 'new',
   'temp': 1
 }
 headers={
   'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit / 537.36(KHTML, like Gecko)Chrome/62.0 3202.62 Safari / 537.36'
 }
 try:
   response=requests.get(root_url,params=params,headers=headers)
 except RequestException:
   return None
 data=json.loads(response.text).get('list')
 img_url_list=[]
 for item in data:
   img_url_list.append(item.get('cover_imgurl'))
 img_url_dict[sn]=img_url_list
#下载图片
def download_image(name,image_url):
 try:
   response=requests.get(image_url)
 except RequestException:
   return "图像请求出错"
 file_name='{}/{}.{}'.format('image',name,'bmp');
 with open(file_name,'wb') as file:
   file.write(response.content)
#获取随机url地址并下载至image文件夹
def get_img():
 sn=30*random.randint(1,15)
 try:
   img_url_dict[sn]
 except KeyError:
   getImgurl('http://image.so.com/zj',sn)
 index=random.randint(0,len(img_url_dict[sn])-1)
 url=img_url_dict[sn][index]
 download_image('wallpaper',url)

二、更换桌面壁纸

第二部分将下载的图片作为壁纸,间隔一定时间重新下载,再切换壁纸
这部分借用python实现windows壁纸定期更换功能


import win32api, win32gui, win32con
import time
def setWallPaper(pic):
 # open register
 regKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
 win32api.RegSetValueEx(regKey,"WallpaperStyle", 0, win32con.REG_SZ, "2")
 win32api.RegSetValueEx(regKey, "TileWallpaper", 0, win32con.REG_SZ, "0")
 # refresh screen
 win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,pic, win32con.SPIF_SENDWININICHANGE)

if __name__=='__main__':
 while True:
   get_img()
   pic='your_path/image/wallpaper.bmp'#写绝对路径
   setWallPaper(pic)
   time.sleep(6)#6s切换一次壁纸

来源:https://blog.csdn.net/weixin_38283159/article/details/79943285

标签:python,桌面壁纸
0
投稿

猜你喜欢

  • 在pycharm中创建django项目的示例代码

    2023-04-07 17:39:25
  • 基于python实现百度翻译功能

    2023-09-06 15:14:18
  • 使用Python进行数独求解详解(一)

    2023-12-25 09:39:20
  • Python实现四个经典小游戏合集

    2021-08-16 12:17:35
  • phpmyadmin显示utf8_general_ci中文乱码的问题终级篇

    2024-04-30 09:57:56
  • 基于python(urlparse)模板的使用方法总结

    2022-10-08 19:56:50
  • Python爬虫制作翻译程序的示例代码

    2023-08-13 06:38:35
  • 解读SQL语句中要不要加单引号的问题

    2024-01-21 06:46:04
  • python连接sql server乱码的解决方法

    2023-06-27 07:25:17
  • CSS? 3D? 3D CSS?

    2009-05-13 13:10:00
  • Swift 3.0在集合类数据结构上的一些新变化总结

    2023-10-19 02:35:47
  • Python数据结构与算法中的栈详解(3)

    2022-01-19 14:38:24
  • python中for语句简单遍历数据的方法

    2023-04-18 11:26:45
  • Python实现图像的二进制与base64互转

    2021-03-18 17:57:55
  • python如何基于redis实现ip代理池

    2022-11-05 20:49:08
  • python回调函数的使用方法

    2023-05-28 02:50:55
  • ASP 精华源码收集(五年总结)第1/20页

    2011-04-07 11:15:00
  • IE下的firebug方法

    2009-07-29 18:50:00
  • go项目打包部署的完整步骤

    2024-05-09 09:46:54
  • asp 批量删除选中的多条记录

    2011-03-29 10:33:00
  • asp之家 网络编程 m.aspxhome.com