python下载每日必应壁纸

#-*-coding:utf-8-*-
from bs4 import  BeautifulSoup
import  requests
import  time
import os
import  PyWallpaper
import win32api
import  win32gui
import  win32con
from PIL import  Image

def set_wallpaper_from_bmp(bmp_path):
    reg_key = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
    win32api.RegSetValueEx(reg_key, "WallpaperStyle", 0, win32con.REG_SZ, "2")
    win32api.RegSetValueEx(reg_key, "TileWallpaper", 0, win32con.REG_SZ, "0")
    win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, bmp_path, win32con.SPIF_SENDWININICHANGE)


def set_wallpaper(file_path):
    bmpImage = Image.open(file_path)
    img_dir = "D:\wallpaper\\"
    print("IMage:dir")
    print(img_dir)
    new_bmp_path = os.path.join(img_dir,'wallpaper.bmp')
    bmpImage.save(new_bmp_path,"BMP")
    set_wallpaper_from_bmp(new_bmp_path)



content = requests.get("https://cn.bing.com/?mkt=zh-CN").text
soup = BeautifulSoup(content,'html.parser')
image_url = "https://cn.bing.com/" + soup.find(id="bgLink")["href"]
print(image_url)
str_time = time.strftime("%Y-%m-%d")
month = time.strftime("%m")
img = requests.get(image_url)

image_path = r"D:\bing\%s"%(month)
image_full_path = r"D:\bing\%s\%s.jpg"%(month,str_time)



if img.status_code == 200:
    folder = os.path.exists(image_path)
    if not folder:
            os.mkdir(image_path)
    with open(r"D:\bing\%s\%s.jpg"%(month,str_time),'wb') as file:
        file.write(img.content)
        set_wallpaper(image_full_path)

你可能感兴趣的:(python下载每日必应壁纸)