爬取必应每日一图并设置为桌面壁纸

使用必应每日一图官方接口获取URL

def getBingImg():
    url = 'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'
    content = requests.get(url).text 
    jsonstr = json.loads(content)
    url = jsonstr["images"][0]["url"]
    imgurl = "https://cn.bing.com" + url
    pic = requests.get(imgurl)
    pic_path = savePic(pic)
    return pic_path

通过https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1可以获取到包含了图片信息url的json字符串信息,得到的字符串信息如下:

{
	'images': [{
		'startdate': '20210821',
		'fullstartdate': '202108211600',
		'enddate': '20210822',
		'url': '/th?id=OHR.OlympicCoast_ZH-CN0827844876_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp',
		'urlbase': '/th?id=OHR.OlympicCoast_ZH-CN0827844876',
		'copyright': '奥林匹克海岸国家海洋保护区的海岸线,美国华盛顿州 (© Chris Moore/Tandem Stills + Motion)',
		'copyrightlink': 'https://www.bing.com/search?q=%E5%A5%A5%E6%9E%97%E5%8C%B9%E5%85%8B%E5%8D%8A%E5%B2%9B&form=hpcapt&mkt=zh-cn',
		'title': '',
		'quiz': '/search?q=Bing+homepage+quiz&filters=WQOskey:%22HPQuiz_20210821_OlympicCoast%22&FORM=HPQUIZ',
		'wp': True,
		'hsh': '110be8dfd6a52bc19cadd1f7bda35f80',
		'drk': 1,
		'top': 1,
		'bot': 1,
		'hs': []
	}],
	'tooltips': {
		'loading': '正在加载...',
		'previous': '上一个图像',
		'next': '下一个图像',
		'walle': '此图片不能下载用作壁纸。',
		'walls': '下载今日美图。仅限用作桌面壁纸。 '}
}

设置为桌面壁纸

def setWallpaper(imagepath):
    print(imagepath)
    k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
    win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸适应桌面,0桌面居中
    win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)

这里主要使用win32api, win32gui, win32con,这里需要注意的是参数需要传完整路径

完整代码

#coding:utf-8
import requests
import time
import json
import os
import win32api, win32gui, win32con
from PIL import Image
from pathlib import Path

def setWallpaper(imagepath):
    print(imagepath)
    k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
    win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2") #2拉伸适应桌面,0桌面居中
    win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,imagepath, 1+2)

def savePic(pic):
    savefolder = Path("./wallpaper/")
    if not savefolder.exists():
        os.mkdir(savefolder)
    name = time.strftime("%Y-%m-%d", time.localtime()) 
    pic_path = os.path.join(os.fspath(savefolder.stem),name + ".jpg")
    file = open(pic_path, "wb")
    file.write(pic.content) 
    return pic_path

def getBingImg():
    url = 'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'
    content = requests.get(url).text 
    jsonstr = json.loads(content)
    print(jsonstr)
    url = jsonstr["images"][0]["url"]
    imgurl = "https://cn.bing.com" + url
    pic = requests.get(imgurl)
    pic_path = savePic(pic)
    return pic_path
    
if __name__ == "__main__":
  #从网站上获取当日的图片
   pic_path = getBingImg()
   
  #d调用windows api设置当前壁纸
   setWallpaper(os.path.join(os.getcwd(),pic_path))

设置开机自启动

1.打开计算机管理
在这里插入图片描述
2.设置任务计划程序
在这里插入图片描述
3.进行任务名称和描述
在这里插入图片描述
4.设置启动时间,我设置为用户登录时
在这里插入图片描述
5.一直点击下一步,这里程序或者脚本需要设置你当前python.exe的位置,不知道的话使用where python获取,参数的话是你python脚本的路径
在这里插入图片描述
6.点击完成,可以编辑属性页
在这里插入图片描述
这个需要勾上,我这边不勾的话好像执行的权限会有问题。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值