我想获取JSON的所有记录,因此我尝试执行以下操作,但是对于单个请求,它可以正常工作,但是当我放入循环中时,它显示了预期的缩进块错误
import requests
url = "https://www.example.com/web/api/Profile/info"
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'Access-Control-Allow-Origin': "*",
'Accept-Encoding': "gzip, deflate",
'Accept-Language': "en-US"
}
n = 10
sum = 0
for i in range(1,n):
payload = "user_id=1"
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text+",")
sum = sum + i
答案 0 :(得分:3)
如回溯中所建议的,您在for循环中缺少缩进;
import requests
url = "https://www.example.com/web/api/Profile/info"
headers = {
'Content-Type': "application/x-www-form-urlencoded",
'Access-Control-Allow-Origin': "*",
'Accept-Encoding': "gzip, deflate",
'Accept-Language': "en-US"
}
n = 10
sum = 0
for i in range(1,n):
payload = "user_id={}".format(i+1)
response = requests.request("POST", url, data=payload, headers=headers)
print(response.text+",")
sum = sum + i