python下载 必应 壁纸

代码:

"""
下载必应壁纸图片
"""

from threading import Thread

import requests


# 继承Thread类创建自定义的线程类
class DownloadHanlder(Thread):

    def __init__(self, url, hashVal):
        super().__init__()
        self.url = url
        self.hashVal = hashVal

    def run(self):
        filename = '/' + self.hashVal + '.jpg'
        resp = requests.get(self.url)
        with open(r'C:\Users\liuren\Desktop\sudy\file' + filename, 'wb') as f:
            f.write(resp.content)


def main():
    # 通过requests模块的get函数获取网络资源
    # 下面的代码中使用了天行数据接口提供的网络API
    # 要使用该数据接口需要在天行数据的网站上注册
    # 然后用自己的Key替换掉下面代码的中APIKey即可
    resp = requests.get(
        'https://raw.onmicrosoft.cn/Bing-Wallpaper-Action/main/data/zh-CN_all.json')
    # 将服务器返回的JSON格式的数据解析为字典
    data_model = resp.json()
    hashVal = 0
    for mm_dict in data_model['data']:
        url = mm_dict['url']
        # hashVal = mm_dict['hsh']
        hashVal += 1
        # 通过多线程的方式实现图片下载
        DownloadHanlder('https://www.bing.com' + url, str(hashVal)).start()


if __name__ == '__main__':
    main()

下载效果:


壁纸

发表评论
留言与评论(共有 0 条评论) “”
   
验证码:

相关文章

推荐文章