如何为列表中的每个项目执行 API 调用

How do I Perform an API Call for each Item in a List

我正在尝试获取元组列表中每个公司的价格数据 [(company_name, symbol)]。在这种情况下,我使用的是 TD Ameritrade API.

此外,我在使用 Reddit 时遇到了同样的问题。唯一的区别是我试图检索每个 post 'id' 的所有评论。但是,使用 Reddit 代码,我从 pandas df 而不是列表中提取 ID。

这是我现在所在的位置:

您尝试提取 ID 列表的方式有误,只需执行以下操作即可:

IDs = df["id"].values.tolist()

之后

IDs = []
for ID in [df["id"]]:
    IDs.append(ID) # Add IDs to ID list

IDS 不是一个int列表,而是pandas.Series一个列表。恰好包含一个系列,即 df["id"].

# This does what you were trying to do:
IDs = []
for ID in df["id"]:
    IDs.append(ID)
    
# Which can be shortened to
IDs = list(df["id"])

# But I think just passing the Series to your function, should work fine:

comments = return_comments_for(df["id"])

真正的错误是 comments 在此之后将是 None,因为 return_comments_for 没有 return 任何东西,所以它会隐式 return None.