仅在 For 循环中获取请求 return 来自最终循环索引的响应。前面的 return 个为空

GET Request in For Loop only returning response from the final loop index. Preceding ones return empty

我 运行 获取 URL 请求,使用文本文件中的行作为所述获取请求中的参数。我发现的问题是 responseind.json() 仅 returning 与 txt 文件的最后一行相关的获取请求。所有前面的行 return []。下面的代码,有什么想法吗?

with open('industries.txt') as industry_file:
       for line in industry_file:
        start = time.time()
        responseind = requests.get("https:URL" + "".join(line) + "?token=My key")
        print(responseind.json())

您的请求无法正常工作的原因是您只是使用了文本文件中的整行(以换行符“\n”结尾)。您的最后一个请求可能得到响应的原因是该 ID 之后没有新行(文件结束),因此没有提供有效 ID 的字符。

with open('industries.txt') as industry_file:
    for line in industry_file:
        start = time.time()
        responseind = requests.get("https:URL" + "".join(line).replace('\n', '') + "?token=My key")
        print(responseind.json())