Last active
April 2, 2020 12:23
-
-
Save imwuwei/d8f89f4d0efcf60c72712b3f88481fa7 to your computer and use it in GitHub Desktop.
每日bing图片下载,默认下载近8天的图片,下载的图片保存在“images”文件夹下
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import os | |
from urllib import request | |
def get_json(): | |
api_url = "http://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=10" | |
res = request.urlopen(api_url).read().decode() | |
data = json.loads(res) | |
return data | |
def get_bing_img(): | |
""" | |
一次性下载八天的bing每日图片,包括当天bing图片和前七天的每日bing图片 | |
文件下载位置位于当前文件路径下的images文件夹 | |
""" | |
try: | |
data = get_json() | |
for i in range(len(data["images"])): | |
startdate = data["images"][i]["startdate"] | |
img_hsh = data["images"][i]['hsh'] | |
img_url = "http://www.bing.com"+data["images"][i]["url"] | |
filename = ("images/%s-%s.%s" %(startdate, img_hsh, "jpg")) | |
request.urlretrieve(img_url, filename) | |
print(filename) | |
except Exception as e: | |
print(e) | |
def get_img(): | |
""" | |
下载今天的每日bing图片 | |
""" | |
try: | |
data = get_json() | |
startdate = data["images"][0]["startdate"] | |
img_hsh = data["images"][0]['hsh'] | |
img_url = "http://www.bing.com"+data["images"][0]["url"] | |
filename = ("images/%s-%s.%s" %(startdate, img_hsh, "jpg")) | |
request.urlretrieve(img_url, filename) | |
print(filename) | |
except Exception as e: | |
print(e) | |
if __name__ == "__main__": | |
try: | |
os.mkdir("images") | |
except Exception as e: | |
print(e) | |
print(os.path.abspath("images")) | |
get_bing_img() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment