Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 现在我有了这个api来生成随机的fox图像,我如何下载这些生成的fox(url)_Python_Python Requests - Fatal编程技术网

Python 现在我有了这个api来生成随机的fox图像,我如何下载这些生成的fox(url)

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\

这是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\/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)