-
Notifications
You must be signed in to change notification settings - Fork 6
/
spider.py
29 lines (23 loc) · 895 Bytes
/
spider.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import json
import shutil
import requests as req
def main():
api_url = 'https://www.bing.com/HPImageArchive.aspx?format=js&idx=0&n=1&mkt=en-US'
api = req.get(api_url)
json_data = json.loads(api.text)
images = json_data['images']
for item in images:
pic_url = r'https://www.bing.com{0}'.format(item['url'])
start_date = item['startdate']
open(r'./json/{0}.json'.format(start_date), 'wb').write(api.content)
pic = req.get(pic_url, stream=True)
if (pic.status_code == 200):
open(f'./images/{start_date}.png', 'wb').write(pic.content)
shutil.copyfile(f'./images/{start_date}.png',
f'./images/latest.png')
print(f'Create {start_date} Image Success!')
else:
print(f'Create {start_date} Image Failed!')
return
if __name__ == "__main__":
main()