Json 使用python和pandas迭代具有不同类别名称的API

Json 使用python和pandas迭代具有不同类别名称的API,json,pandas,api,for-loop,request,Json,Pandas,Api,For Loop,Request,我正在做一个学校项目,在那里我需要迭代16个类别(我已经在一个班级列表中有了它们)来获取API中的所有笑话,而我似乎做不到这一点 我知道我需要对每个类别迭代一次请求(在本例中,我已经知道它们是16,并且我有一个名为“r”的变量存储它们) 我想我需要使用,但似乎无法使用。在获取16个类别的完整列表的基础上,我需要将它们全部放在一个新的数据框架中 ??是要获得的类别名称(0:“动物”,1:“职业”,2:“名人”,3:“开发”,4:“明确”,5:“时尚”,6:“食品”,7:“历史”,8:“金钱”,9:

我正在做一个学校项目,在那里我需要迭代16个类别(我已经在一个班级列表中有了它们)来获取API中的所有笑话,而我似乎做不到这一点

我知道我需要对每个类别迭代一次请求(在本例中,我已经知道它们是16,并且我有一个名为“r”的变量存储它们)

我想我需要使用,但似乎无法使用。在获取16个类别的完整列表的基础上,我需要将它们全部放在一个新的数据框架中

??是要获得的类别名称(0:“动物”,1:“职业”,2:“名人”,3:“开发”,4:“明确”,5:“时尚”,6:“食品”,7:“历史”,8:“金钱”,9:“电影”,10:“音乐”,11:“政治”,12:“宗教”,13:“科学”,14:“体育”,15:“旅游”)

提前thx

----更多详细信息(编辑)

获取类别名称的API:

import requests

url = "https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/categories"

headers = {
    'accept': "application/json",
    'x-rapidapi-key': "xxxx",
    'x-rapidapi-host': "matchilling-chuck-norris-jokes-v1.p.rapidapi.com"
    }

response = requests.request("GET", url, headers=headers)
a = response.text
a
输出: “[“动物”、“职业”、“名人”、“发展”、“明确”、“时尚”、“食品”、“历史”、“金钱”、“电影”、“音乐”、“政治”、“宗教”、“科学”、“体育”、“旅游”]

然后,我不再手动查询每个类别,而是试图找到一种方法来迭代所有类别,并将所有输出放在一个数据帧中

import requests

url = "https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/search"

querystring = {"query":"animal"}

headers = {
    'accept': "application/json",
    'x-rapidapi-key': "xxxxxx",
    'x-rapidapi-host': "matchilling-chuck-norris-jokes-v1.p.rapidapi.com"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)
df_chuck_categories = pd.DataFrame(r, columns=['category'])

希望我能够更好地解释我要完成的内容。thx提前

不清楚??中包含了什么内容,但您需要迭代列表。假设您的“r”是字典,请使用r.keys()或r.values()。这会将所有响应收集到一个列表中。然后,您可以将它们处理为数据帧并连接。如果不知道response.text格式,则无法明确说明如何将其转换为数据帧

url = "https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/search"
headers = {
    'accept': "application/json",
    'x-rapidapi-key': "xxxxx",
    'x-rapidapi-host': "matchilling-chuck-norris-jokes-v1.p.rapidapi.com"
    }
response_list = []
for s in list(r.keys()):
    querystring = {"query":s}
    response = requests.request("GET", url, headers=headers, params=querystring)
    print(response.text)
    response_list.append(response.text) 

首先,我将响应列表转换为数据帧

import requests

url = "https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/search"

querystring = {"query":"animal"}

headers = {
    'accept': "application/json",
    'x-rapidapi-key': "xxxxxx",
    'x-rapidapi-host': "matchilling-chuck-norris-jokes-v1.p.rapidapi.com"
    }

response = requests.request("GET", url, headers=headers, params=querystring)

print(response.text)
df_chuck_categories = pd.DataFrame(r, columns=['category'])
使用@Jonathan Leon,它工作得非常好。for s in正在进行迭代,API正在返回所有类别

import requests

url = "https://matchilling-chuck-norris-jokes-v1.p.rapidapi.com/jokes/search"
headers = {
    'accept': "application/json",
    'x-rapidapi-key': "xxxxxxx",
    'x-rapidapi-host': "matchilling-chuck-norris-jokes-v1.p.rapidapi.com"
    }
response_list = []
for s in list(df_chuck_categories['category']):
    querystring = {"query":s}
    response = requests.request("GET", url, headers=headers, params=querystring)
    print(response.text)
    response_list.append(response.text) 

您好,类别名称出现在??中。如果我一次手动编写一个类别名称,我最终将获得我需要的所有值。我想做的是使用一个函数来迭代所有类别名称,而不是手动为每个类别进行迭代。然后将所有结果合并到一个数据帧中