python3 抓取必应bing首页图片作为桌面背景

本文介绍了一款使用Python编写的简单程序,该程序能够从必应网站抓取每日壁纸图片,并自动设置为Windows系统的桌面背景。代码适用于Python 3.5.1版本,已在Win10 x64系统上测试通过。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上找到的在python3下都不能用,于是动手修改了一下,测试可用。

Python 版本:3.5.1
系统:win10 x64
需要安装的包:

pip install pypiwin32

代码:

import os
import random
import urllib.request
import win32gui
import win32con
from PIL import Image

class StealBing:

    def __init__(self):
        self.content = urllib.request.urlopen('http://cn.bing.com/').read()
        self.bgImageUrl = ''
        self.localFileName = ''
        self.localBMPFileName = ''

    def parserImageURL(self):
        tempStr = str(self.content)
        startIndex = tempStr.index('g_img={url:')+13
        endIndex = tempStr.index(',id:')-1
        tempStr = tempStr[startIndex:endIndex]
        tempStr = tempStr.replace('\\', '')
        self.bgImageUrl = tempStr
        print(tempStr)

    #仅用于生成本地文件名,在这里修改保存的路径
    def createLocalFileName(self):   
        randomStr = ''.join(random.sample(['a','b','c','d','e','f','g','h','i','j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'], 6)).replace(" ","")
        self.localFileName =  'e:/desktop/background/' + randomStr + '.jpg'
        self.localBMPFileName = 'e:/desktop/background/' + randomStr + '.bmp'

    def downloadImage(self):
        if self.bgImageUrl == '':
            self.parserImageURL()
        if self.localFileName == '':
            self.createLocalFileName()
        urllib.request.urlretrieve(self.bgImageUrl, self.localFileName)


    def updataBGImage(self):
        img = Image.open(self.localFileName)
        img.save(self.localBMPFileName)
        os.remove(self.localFileName)
        win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER, self.localBMPFileName, 0)

if __name__ == '__main__':
    stealBing = StealBing()
    stealBing.downloadImage()
    stealBing.updataBGImage()

直接下载http://download.youkuaiyun.com/download/busyluo/9369926

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值