还是直接上代码:
import requests
import os
import json
for i in range(1,200):
res = requests.get("https://dog.ceo/api/breeds/image/random");
print(res.status_code)
print(res.text)
json2python = json.loads(res.text)
default_info = '当前的 key 不存在'
url = json2python.get('message', default_info)
print(url)
h={'User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/99.0.4844.51 Mobile Safari/537.36'}
d='D:\\A\\'
path=d+"f"+ str(i)+".jpg"
print(path)
if not os.path.exists(d):
os.mkdir(d)
r=requests.get(url)
if(r.status_code==200):
r.raise_for_status()
with open(path,'wb') as f:
f.write(r.content)
f.close()
print("保存成功")