调用bing图片html代码,开机获取bing每日图片并设为桌面壁纸【含源代码】

[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)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值