Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/295.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请求分页_Python_Python Requests - Fatal编程技术网

如何修复不起作用的python请求分页

如何修复不起作用的python请求分页,python,python-requests,Python,Python Requests,我试图用python请求分页,但我似乎无法获得总页面,而且JSON响应也不会随着页面的更改而更新 url='1〕https://www.walmart.com/store/1003-York-pa/search?query=ice%20cream' 美国石油学会https://www.walmart.com/store/electrode/api/search' 参数={ “查询”:单词, “cat_id”:0, ‘附言’:24, “偏移量”:0, “prg”:“桌面”, 'stores':重新

我试图用python请求分页,但我似乎无法获得总页面,而且JSON响应也不会随着页面的更改而更新

url='1〕https://www.walmart.com/store/1003-York-pa/search?query=ice%20cream'
美国石油学会https://www.walmart.com/store/electrode/api/search'
参数={
“查询”:单词,
“cat_id”:0,
‘附言’:24,
“偏移量”:0,
“prg”:“桌面”,
'stores':重新搜索(r'store/(\d+),url).组(1)
}
data1=requests.get(api_url,params=params).json()
num_pages=data1['pagination']['pages'][-1]['pages']
对于范围内的页面(0,num_pages+1):
参数={
“查询”:单词,
“cat_id”:0,
“页面”:页面,//尝试更新新页面
‘附言’:24,
“偏移量”:0,
“prg”:“桌面”,
'stores':重新搜索(r'store/(\d+),url).组(1)
}
data=requests.get(api_url,params=params).json()

我做错了什么,如何修复它?

在第二个
参数
设置(内部for循环)中,您需要删除
'offset':0,
,因为它每次都指示服务器从第一页的第一个结果提取。这是唯一的主要错误。在此修复之后,我开始为每个页面请求获取不同的产品

其次,for循环应该从
1
开始,因为页面是从1开始计算的,正如我在响应中看到的那样


第三个可能是第二个
请求。get
调用应该位于循环的分页内,或者您只是复制了缩进错误的粘贴代码。

最后一行的缩进错误,需要将其嵌套在
for
循环内。通过这种方式,它通过循环,然后只对最新页面执行单个请求。