程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python - Get Bing Homepage Wallpaper Automatically

編輯:Python

一.引言

Bing The wallpapers on the homepage are beautiful and updated daily,下面介紹如何使用 python Automatically get wallpapers daily and save.

二.手動獲取

Before obtaining it automatically, we will introduce how to obtain it manually,Mainly to understand the web form of wallpaper.

1.打開開發者模式

可以直接 F12 Shortcut key to enter development mode,found in the right column s.cn.bing.net 選項

2.打開新的 Tab

Double-click the corresponding location to select open in new Tab

 You can get the full wallpaper,Right-click and select Save As to save to the specified location:

Tips:

The URL obtained here is connected to:

https://s.cn.bing.net/th?id=OHR.LongsPeak_EN-CN6019073969_1920x1080.jpg&rf=LaDigue_1920x1080.jpg

其中 https://s.cn.bing.net 為前綴,/th?id=OHR.LongsPeak_EN-CN6019073969_1920x1080.jpg&rf=LaDigue_1920x1080.jpg suffix for the image,The wallpaper address can be obtained by splicing the two together,後續通過 python The crawler also obtains the wallpaper based on this address.

三.自動獲取

1.官方API

Bing 官方提供 API Get online web wallpapers:官方API

https://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=zh-CN

 主要有 format、idx、n、mkt 四個參數:

參數含義format返回數據形式 js - json xml - xmlidx截止天數 0-今天 -1 - Until tomorrow 1 截止至昨天n返回數量        mkt地區 zh-CN - 國區

測試過程中發現 n The number is always returned1.

2.Postman 調用接口

使用 Postman Get Api Check the interface return below json the rough form of,沒有 postman It does not affect the subsequent acquisition of wallpapers,Get wallpapers just need python 即可.

You can see the details of the current wallpaper,The address corresponding to the wallpaper is:"Longs Peak in Rocky Mountain National Park,科羅拉多州 ( Andrew R. Slaton/Tandem Stills + Motion),非常的漂亮.其 images Also included in the array url ,該 url 形式為:

 "url": "/th?id=OHR.LongsPeak_ZH-CN5927119555_1920x1080.jpg&rf=LaDigue_1920x1080.jpg&pid=hp"

Open with the one we just looked for manually tab The address only differs from the prefix https://s.cn.bing.net:

https://s.cn.bing.net/th?id=OHR.LongsPeak_EN-CN6019073969_1920x1080.jpg&rf=LaDigue_1920x1080.jpg

所以 python The execution logic is relatively clear:

A.調用 API 獲取 Json

B.通過 Json Get the wallpaper address,Splice the prefix to get the final wallpaper address

C.將對應 content 生成 jpg Save to this unit

3.Python 實現

#!/usr/bin/python
# -*- coding: utf-8 -*-
import requests
import json
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36",
"Connection": "close",
}
def dumpBingWallpaper():
# 解析 URL
n = 1
idx = 1
url = "https://www.bing.com/HPImageArchive.aspx?format=js&idx={}&n={}".format(idx, n)
res = requests.get(url, headers=headers)
res.encoding = 'utf8'
jsonData = json.loads(res.text)
uri = jsonData['images'][0]['url']
# Get image address and information
img = requests.get("https://s.cn.bing.net/" + uri, headers=headers).content
desc = str(jsonData['images'][0]['copyright']).split(",")[0]
dt = jsonData['images'][0]['startdate']
# 輸出地址
output = '/Users/xudong11/Desktop/{}.jpg'.format(desc + "_" + dt)
out = open(output, 'wb')
out.write(img)
out.close()
if __name__ == "__main__":
dumpBingWallpaper()

通過 copyright 和 startdate Get the image profile and date as the name of the output image,After running, get the target wallpaper at the specified location:

Tips:

通過 chorm The method for obtaining image information by the developer tool requires the introduction of the developer toolkit,Interested students can also do it:

from selenium import webdriver
options = webdriver.ChromeOptions()
Chrome = webdriver.Chrome(options=options, desired_capabilities=capabilities)
wait = WebDriverWait(Chrome, 5)

4.定時執行

A.定時腳本

First add timing shell 腳本 run.sh,PWD 為 python 所在文件夾目錄:

#!/bin/bash
path=${PWD}
cd $path
python DumpBingPic.py

B.crontab 定時啟動

20 11 * * * 代表每天 11:20 下載 Bing 圖片:

20 11 * * * source ~/.bash_profile && cd ${PWD} && sh run.sh

No need to run the script manually,Save every day bing 壁紙,非常的奈斯

四.Windows 聚焦

除了 Bing 壁紙,之前也整理過 windows How to get the focused boot wallpaper,並最終生成 .exe 的可執行文件,windows Students who are interested can continue to refer to it:Python 提取WindosFocused landing image,運行 exe to get the most recent windows Focused boot wallpaper:


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved