python3 抓取必應bing首頁圖片做爲桌面背景

網上找到的在python3下都不能用,因而動手修改了一下,測試可用。python

Python 版本:3.5.1
系統:win10 x64
須要安裝的包:web

pip install pypiwin32

代碼:dom

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.csdn.net/download/busyluo/9369926svg