python - 在 python 中,如果 api url 在 csv 文件中,则无法发送 GET 请求
问题描述
我无法使用 pythons 发送获取请求,通过 csv 文件提供 api url,如果它们满足然后这个 api 运行在 csv 中有一些条件,在运行时在终端中找不到响应数据
with open('xyz.csv', mode='r', newline='') as csvFile :
reader= csv.reader(csvFile)
for col in reader:
if col[2].upper() == "Y" and col[9].upper() == "Y":
r = requests.get(col[6],headers=headers)
resp = json.loads(resp)
elif resp.status_code == 200:
print(resp.content)
else:
pass
解决方案
如果请求正确,您的打印将不会运行,因为它位于错误的if
块中!
当第一个if
被执行时,你的elif
将不会被输入(这就是 if 的工作方式!)。这意味着,您的请求已发送,if
但您无法打印结果。此外,如果您在某处有一个正常的请求,然后首先if
不起作用,您将打印您的最后一个结果 - 您必须缩进这个。
with open('xyz.csv', mode='r', newline='') as csvFile :
reader= csv.reader(csvFile)
for col in reader:
if col[2].upper() == "Y" and col[9].upper() == "Y":
r = requests.get(col[6],headers=headers)
resp = json.loads(resp)
if resp.status_code == 200: #normal if and indent
print(resp.content) #indented as well