爬取bing每日美图作为桌面背景

版权声明:https://blog.csdn.net/thfyshz版权所有 https://blog.csdn.net/thfyshz/article/details/83472172
  • 需先安装pypiwin32,可通过如下代码安装:
pip install pypiwin32

完整代码:

import re
import win32gui, win32con, win32api
from urllib import request

url = "https://cn.bing.com/"
headers = {'User-Agent': 'Mozilla/5.0 (Linux; Android 4.1.1; Nexus 7 Build/JRO03D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166  Safari/535.19'}
req = request.Request(url, headers=headers)
string = request.urlopen(req).read().decode('utf8')

url_pic = 'https://cn.bing.com/' + re.findall('\/script><\/html><img src=\"(.*)\" style=', string)[0]

imagepath = 'D:/1.jpg'
request.urlretrieve(url_pic, imagepath)

# 打开指定注册表路径
k = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER, "Control Panel\\Desktop", 0, win32con.KEY_SET_VALUE)
# 最后的参数:2拉伸,0居中,6适应,10填充,0平铺
win32api.RegSetValueEx(k, "WallpaperStyle", 0, win32con.REG_SZ, "2")
# 最后的参数:1表示平铺,拉伸居中等都是0
win32api.RegSetValueEx(k, "TileWallpaper", 0, win32con.REG_SZ, "0")
# 刷新桌面
win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, imagepath, win32con.SPIF_SENDWININICHANGE)

猜你喜欢

转载自blog.csdn.net/thfyshz/article/details/83472172