Python3爬取Bing每日圖片,並設置爲電腦桌面

作爲鄙視鏈底層的“腳本小子”,到處找源碼然後自己修改的小菜鳥,今天記錄使用Python爬取必應每日圖片,保存後設置爲電腦圖片的小項目。

1 - 簡述

本文使用Python3,爬取BingImage作爲電腦桌面壁紙。

將爬取的圖片以當前日期命名,存入BingImg文件夾並設置爲電腦桌面(運行程序自動創建該文件夾)。

bing.com每天更換一張背景圖,大部分都屬佳品,用來當作壁紙十分合適。但是這個腳本只能是運行一次就設置一次。爲了解決這個問題,我們可以給系統設置一個定時任務,每天觸發一次。這樣就可以實現每天自動更換BingImage作爲電腦桌面了。

2 - 核心代碼

2.1 - 爬取BingImage

def getImage(local):
	url = 'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'  # 調用的 json 文件(format=js)
	os.makedirs('BingImg', exist_ok=True)  # 生成 BingImg 文件夾用來保存圖片

	con = requests.get(url)
	content = json.loads(con.text)
	picUrl = 'https://cn.bing.com' + content['images'][0]['url']
	if picUrl == '':
	    print('找不到圖片!程序出錯啦!')
	    sys.exit()
	else:
	    print('獲取圖片地址成功:' + picUrl)
	    print('開始下載···')  
	    read = requests.get(picUrl)

	f = open(os.path.join('BingImg', '%s.jpg' % local), 'wb')
	f.write(read.content)
	f.close()
	print('下載成功!')

2.2 - 設置爲桌面

def setWallPaper(pic):
  print('正在更新桌面背景.....')
  regKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
  win32api.RegSetValueEx(regKey,"WallpaperStyle", 0, win32con.REG_SZ, "2")
  win32api.RegSetValueEx(regKey, "TileWallpaper", 0, win32con.REG_SZ, "0")
  # refresh screen
  win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,pic, win32con.SPIF_SENDWININICHANGE)
  print('更新完遼!')
  print('-'*20)
  print('開心麼?\n鵝鵝鵝餓鵝鵝鵝餓.....')

2.3 - 設置爲每日自動執行

設置步驟:
右擊“計算機”→管理→計劃任務程序→創建基本任務

接下來按照指引完成設置即可。
在這裏插入圖片描述

3 - 完整代碼


# 爬取今日Bing Image並設置爲桌面

import sys
import os
import time
import json
import requests
from PIL import Image 
import win32api, win32gui, win32con


def getImage(local):
	url = 'https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'  # 調用的 json 文件(format=js)
	os.makedirs('BingImg', exist_ok=True)  # 生成 BingImg 文件夾用來保存圖片

	con = requests.get(url)
	content = json.loads(con.text)
	picUrl = 'https://cn.bing.com' + content['images'][0]['url']
	if picUrl == '':
	    print('找不到圖片!程序出錯啦!')
	    sys.exit()
	else:
	    print('獲取圖片地址成功:' + picUrl)
	    print('開始下載···')  
	    read = requests.get(picUrl)

	f = open(os.path.join('BingImg', '%s.jpg' % local), 'wb')
	f.write(read.content)
	f.close()
	print('下載成功!')


def showImage(imgName,local):
	img = Image.open('%s/BingImg/%s.jpg'%(imgName,local))
	img.show()


def setWallPaper(pic):
  print('正在更新桌面背景.....')
  regKey = win32api.RegOpenKeyEx(win32con.HKEY_CURRENT_USER,"Control Panel\\Desktop",0,win32con.KEY_SET_VALUE)
  win32api.RegSetValueEx(regKey,"WallpaperStyle", 0, win32con.REG_SZ, "2")
  win32api.RegSetValueEx(regKey, "TileWallpaper", 0, win32con.REG_SZ, "0")
  # refresh screen
  win32gui.SystemParametersInfo(win32con.SPI_SETDESKWALLPAPER,pic, win32con.SPIF_SENDWININICHANGE)
  print('更新完遼!')
  print('-'*20)
  print('開心麼?\n鵝鵝鵝餓鵝鵝鵝餓.....')


if __name__=='__main__':
	local = time.strftime("%Y-%m-%d")  # 以當天日期命名文件
	imgName = os.getcwd().replace("\\","/") # 獲取當前文件夾地址
	#print(local)
	getImage(local)
	#showImage(imgName,local)
	pic = '%s/BingImg/%s.jpg'%(imgName,local)	# 獲取圖片路徑
	setWallPaper(pic)

4 - 運行結果

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章