For循环中的GET请求仅返回来自最终循环索引的响应.前一个返回空



我正在运行对URL的get请求,在所述get请求中使用文本文件中的行作为参数。我发现的问题是responseind.json((只返回与txt文件的最后一行相关的get请求。前面的所有行都返回[]。下面的代码,有什么想法吗?

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())

最新更新