[Python] 纯文本查看 复制代码import requests, os, parsel, time, sys, ctypes, subprocess
def changePaper(img_path):
# 设置壁纸
ctypes.windll.user32.SystemParametersInfoW(20, 0, img_path, 0)
command = 'reg add \"hkcu\\control panel\\desktop\" /v \"wallpaper\" /d \"' + img_path + '\" /f'
subprocess.run(command, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
'''
刷新
'RunDll32.exe USER32.DLL,UpdatePerUserSystemParameters'
'''
def isConnected():
# 测试是否有网
try:
requests.get("https://cn.bing.com", timeout=2)
except:
return False
return True
Date = time.strftime("%Y-%m-%d", time.localtime())
MainPath = os.path.dirname(os.path.realpath(sys.argv[0])) + '\\Wallpapers'
paperPath = MainPath + '\\' + Date + '必应壁纸.jpg'
def getPaper():
# 下载壁纸
try:
link = 'https://cn.bing.com'
headers = {'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko'}
response_str = requests.get(link, headers).text
html = parsel.Selector(response_str)
li = html.xpath('//link[@id="bgLink"]/@href').get()
url = link + li
img_data = requests.get(url, headers=headers).content
with open(paperPath, 'wb') as f:
f.write(img_data)
except :
pass
if os.path.exists(paperPath):
changePaper(paperPath)
else:
if not os.path.exists(MainPath):
os.makedirs(MainPath)
for i in range(100):
if isConnected():
getPaper()
changePaper(paperPath)
break
time.sleep(3)