鱼C论坛

 找回密码
 立即注册
查看: 1469|回复: 1

[已解决]python爬虫怎么翻页呢,怎么知道有几页

[复制链接]
发表于 2023-10-23 21:38:16 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

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'])
最佳答案
2023-10-23 21:38:25
要翻页,你需要知道有多少页数据可用。在这种情况下,你可以通过检查返回的数据中的页数来确定有多少页。

在你的代码中,你可以使用以下方法来获取页数:

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数据中提取总页数。然后,它将使用一个循环来遍历每一页的数据,并进行相应的处理。

注意:根据你的具体需求,你可能还需要添加一些错误处理和异常处理的代码。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-10-23 21:38:25 | 显示全部楼层    本楼为最佳答案   
要翻页,你需要知道有多少页数据可用。在这种情况下,你可以通过检查返回的数据中的页数来确定有多少页。

在你的代码中,你可以使用以下方法来获取页数:

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数据中提取总页数。然后,它将使用一个循环来遍历每一页的数据,并进行相应的处理。

注意:根据你的具体需求,你可能还需要添加一些错误处理和异常处理的代码。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-9-21 13:53

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表