You need to enable JavaScript to run this app. Python BeautifulSoup爬取FIFA2020球员数据时如何触发next按钮翻页
优惠活动
大模型
产品
解决方案
定价
更多

Python BeautifulSoup爬取FIFA2020球员数据时如何触发next按钮翻页

核心跨页抓取实现思路

1. 不要浪费时间模拟按钮点击

sofifa的下一页跳转是标准的URL参数驱动,没有复杂的前端JS异步加载逻辑,右键检查页面上的“next”按钮元素,就能看到它的固定跳转规则:

  • 单页固定展示60条球员数据,分页靠offset参数控制内容偏移量
  • 第一页默认从第0条开始展示,对应offset=0;第二页跳过前60条,对应offset=60;第三页offset=120,以此类推
  • 你原来URL里的版本号、自定义展示列的参数全部保留不动,只需要动态修改offset的值就能拿到对应分页的内容,完全不需要引入Selenium这类重型工具,和你现在用的requests+BeautifulSoup技术栈完全适配。

2. 复用现有单页提取逻辑,搭分页循环

你已经调试通过的单页字段提取代码不需要改动,只要把这部分逻辑放到分页循环里,每次拿到不同offset的页面HTML,就跑一遍提取流程,把每一页的结果追加到全局存储列表里就行,最后统一生成DataFrame。

3. 加好循环终止条件

避免无限制爬取,必须设置终止判断:当当前页面解析不到带有效跳转链接的next按钮时,说明已经到了最后一页,直接跳出循环。最后一页的next按钮是禁用状态,没有可跳转的href属性,很容易识别。

适配你现有代码的优化版本

直接复用你已经写通的单页逻辑,没有额外学习成本:

import requests
from bs4 import BeautifulSoup
import pandas as pd
import time

# 全局存储所有分页的球员数据
all_player_records = []
headers = {
    'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'
}
# 基础URL,保留你设置的所有展示列、FIFA2020版本参数
base_url = "https://sofifa.com/?r=200061&set=true&showCol%5B%5D=ae&showCol%5B%5D=oa&showCol%5B%5D=pt&showCol%5B%5D=vl&showCol%5B%5D=hi&showCol%5B%5D=wi&showCol%5B%5D=pf&showCol%5B%5D=bo&showCol%5B%5D=pi"
offset = 0

while True:
    # 拼接当前分页的完整请求地址
    current_url = f"{base_url}&offset={offset}"
    resp = requests.get(current_url, headers=headers)
    soup = BeautifulSoup(resp.content, 'html.parser')

    # 完全复用你已经调试通过的字段提取逻辑
    Players = soup.find_all("a", {"class": "tooltip"})
    Age = soup.find_all("td", {"class": "col col-ae"})
    OR = soup.find_all("td", {"class": "col col-oa col-sort"})
    PR = soup.find_all("td", {"class": "col col-pt"})
    Team = soup.find_all("div", {"class": "bp3-text-overflow-ellipsis"})
    contract = soup.find_all("div", {"class": "sub"})
    height = soup.find_all("td", {"class": "col col-hi"})
    weight = soup.find_all("td", {"class": "col col-wi"})
    PF = soup.find_all("td", {"class": "col col-pf"})
    Value = soup.find_all("td", {"class": "col col-vl"})

    j = 1
    for i in range(0, 60):
        record = {
            "Name": Players[i].text.strip(),
            "Age": Age[i].text.strip(),
            "Overall Rating": OR[i].text.strip(),
            "Potential": PR[i].text.strip(),
            "Team": Team[i + j].text.strip(),
            "Contract expiry": contract[i].text.strip(),
            "Height": height[i].text.strip(),
            "Weight": weight[i].text.strip(),
            "Strong foot": PF[i].text.strip(),
            "Value": Value[i].text.strip()
        }
        all_player_records.append(record)
        j += 1

    # 检查是否存在有效下一页按钮
    next_btn = soup.find("a", string="next")
    # 没有next按钮,或者按钮没有跳转链接(最后一页禁用状态)就终止循环
    if not next_btn or not next_btn.get("href"):
        break

    # 偏移量加60,准备爬下一页
    offset += 60
    # 每页间隔1秒,避免请求过快被站点封禁
    time.sleep(1)

# 全量数据爬完后生成DataFrame
df = pd.DataFrame(all_player_records)
# 导出为CSV文件即可本地留存
df.to_csv("fifa2020_full_players.csv", index=False)
实操注意点
  • 代码里加了strip()方法清理提取到的文本前后的空白字符,比原来直接取text的效果更好,不会出现带多余换行、空格的脏数据
  • 延时不要设的比1秒更短,sofifa对高频请求的限制比较严格,爬取全量球员数据全程也就十几分钟,没必要抢那点速度
  • 如果中途遇到网络请求失败的情况,可以简单加个异常捕获,连续重试2-3次还是失败就跳过当前页,不影响整体数据完整性

内容的提问来源于stack exchange,提问作者davvpo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.20 08:24:04