由于上一版的接口并不是很稳定,出现过多次获取失败的情况。
重新找了个接口,思路跟之前差不多。只不过新接口是在 在线的 json 文件 中,需要先将json 文件下载下来,再进行获取图片地址。


import json
import jsonpath
from tokenize import String
import urllib.request
from lxml import etree
import time
import os
import ctypes

    
def get_content():

    urllib.request.urlretrieve(url,'D:/IDM download/images/'+ name +'.json')


def down_load():

    jsonname = 'D:/IDM download/images/'+ name +'.json'

    obj = json.load(open(jsonname,'r',encoding='utf-8'))
    # print(obj)
    src = jsonpath.jsonpath(obj,'$.images[*].url')
    

    url = 'https://cn.bing.com/'+ src[0]
    print(url)

    filename = 'D:/IDM download/images/'+ name + '.jpg'
    print(filename)
    urllib.request.urlretrieve(url=url,filename=str(filename))

    if os.path.exists(jsonname):
        os.remove(jsonname)
    else:
        print("文件不存在")

    return filename

if __name__ == '__main__':
    url = 'http://cn.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1'
    name = time.strftime("%Y%m%d",time.localtime())

    # 1.下载 json 文件
    get_content()

    # 2.下载 图片
    filename = down_load()
    # 3. 设置为桌面壁纸
    ctypes.windll.user32.SystemParametersInfoW(20, 0, filename , 0)

github :https://github.com/ineac/Bing_Wallpaper

文章作者: 临川
版权声明: 本站所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来自 临川羡鱼
Python Python
喜欢就支持一下吧