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

Iterating through an API with different category names using python and pandas

我正在做一个学校项目,我需要遍历 16 个类别(我已经将它们放在 class 列表中)以获取 API 中的所有笑话,我可以好像也这么干。

我知道我需要为每个类别重复请求一次(在这种情况下,我已经知道它们是 16,并且我有一个名为“r”的变量存储它们。

我想我需要使用 for 但似乎做不到。除了获取 16 个类别的完整列表之外,我还需要将它们全部放入一个新的数据框中。

?? = 是要获取的类别名称 (0:"animal", 1:"career", 2:"celebrity", 3:"dev", 4:"explicit", 5:"fashion", 6:"food", 7:“历史”,8:“金钱”,9:“电影”,10:“音乐”,11:“政治”,12:“宗教”,13:“科学”,14:“体育”,15: "旅行")

import requests

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

querystring = {"query":"??"}

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

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

print(response.text)

提前谢谢

---- 更多细节(编辑)

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)

希望我能够更好地解释我正在努力完成的事情。提前谢谢

不清楚 ??但你需要遍历一个列表。假设你的 'r' 是字典使用 r.keys() 或 r.values()。这会将所有响应收集到一个列表中。然后,您可以将它们操作成数据帧并连接起来。在不知道 response.text 格式的情况下,无法明确说明如何将其转换为 pandas.

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) 

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

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)