|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- import requests
- import json
- for page in range(1,)
- url='http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
- location=input('输入你想要查询的地名:')
- param={'cname': '','pid':'', 'keyword':location,'pageIndex':page,'pageSize': '10'}
- headers={'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.4031 SLBChan/105'}
- response=requests.post(url=url,data=param,headers=headers)
- list=response.json()
- for i in list['Table1']:
- print(i['cityName']+i['storeName'],i['addressDetail'])
复制代码
要翻页,你需要知道有多少页数据可用。在这种情况下,你可以通过检查返回的数据中的页数来确定有多少页。
在你的代码中,你可以使用以下方法来获取页数:
1. 在发送POST请求后,获取响应对象response。
2. 使用response的json()方法将响应内容转换为JSON格式。
3. 从JSON数据中提取页数信息。
以下是修改后的代码示例:
- import requests
- import json
- url = 'http://www.kfc.com.cn/kfccda/ashx/GetStoreList.ashx?op=keyword'
- location = input('输入你想要查询的地名:')
- # 初始化页数为1
- page = 1
- param = {'cname': '', 'pid': '', 'keyword': location, 'pageIndex': page, 'pageSize': '10'}
- headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.131 Safari/537.36 SLBrowser/8.0.1.4031 SLBChan/105'}
- response = requests.post(url=url, data=param, headers=headers)
- data = response.json()
- # 获取总页数
- total_pages = data['Table'][0]['Table1'][0]['rowcount'] // 10 + 1
- print('总页数:', total_pages)
- # 进行翻页操作
- for page in range(1, total_pages + 1):
- param['pageIndex'] = page
- response = requests.post(url=url, data=param, headers=headers)
- data = response.json()
- # 处理每一页的数据
- for item in data['Table1']:
- print(item['cityName'] + item['storeName'], item['addressDetail'])
复制代码
这段代码将首先发送一个POST请求来获取第一页的数据,并从返回的JSON数据中提取总页数。然后,它将使用一个循环来遍历每一页的数据,并进行相应的处理。
注意:根据你的具体需求,你可能还需要添加一些错误处理和异常处理的代码。
|
|