Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python中的for循环出错。。循环工作不正常_Python_Python Requests - Fatal编程技术网

python中的for循环出错。。循环工作不正常

python中的for循环出错。。循环工作不正常,python,python-requests,Python,Python Requests,我想获取JSON的所有记录,所以我尝试执行以下操作,但对于单个请求,它工作得很好,但当我放入循环时,它显示预期的缩进块错误 import requests url = "https://www.example.com/web/api/Profile/info" headers = { 'Content-Type': "application/x-www-form-urlencoded", 'Access-Control-Allow-Origin': "*", 'Acce

我想获取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

正如回溯中所建议的,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

但是如何循环有效载荷?是的是的闪光!快开枪@Amitkumar你已经硬编码了
有效载荷
,所以它总是一样的,你想如何循环它?@MattB。是的,我想循环它以获取所有10条记录