Python 现在我有了这个api来生成随机的fox图像,我如何下载这些生成的fox(url)
这是api- 这是我的代码:-Python 现在我有了这个api来生成随机的fox图像,我如何下载这些生成的fox(url),python,python-requests,Python,Python Requests,这是api- 这是我的代码:- import requests import wget response = requests.get("https://randomfox.ca/floof/") fox = response.json() print(fox['image']) wget.download(fox) 现在它输出了一个url,现在我如何下载生成的图像,请首先帮助,查看url给我以下信息: {“image”:“https:\/\/randomfox.ca\
import requests
import wget
response = requests.get("https://randomfox.ca/floof/")
fox = response.json()
print(fox['image'])
wget.download(fox)
现在它输出了一个url,现在我如何下载生成的图像,请首先帮助,查看url给我以下信息:
{“image”:“https:\/\/randomfox.ca\/images\/110.jpg”,“link”:“https:\/\/randomfox.ca\/?i=110”}
当您打印fox['image']
时,您所做的只是打印它,但它不是一个变量。要解决此问题,请调整代码,如下所示:
response = requests.get("https://randomfox.ca/floof/")
fox = response.json()
url = fox['image']
wget.download(url)
其次,没有必要使用wget
,因为请求可以处理所有这些
import requests
response = requests.get("https://randomfox.ca/floof/")
fox = response.json()
url = fox['image']
r = requests.get(url)
with open('image.png','wb') as f:
f.write(r.content)