You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何用BeautifulSoup的find_all过滤曼联英超胜场结果?

提取曼联最新10场赛事中的胜场结果

你的问题出在当前选择的wcl-simpleText_Asp-0 wcl-scores-simpleText-01_pV2Wk是所有赛事比分的通用类,所以会抓取所有10场的比分,无法区分胜负。要筛选胜场,需要先定位单场赛事的容器,再通过胜负状态标识或比分对比来判断是否为胜场。

解决方案步骤:

  1. 等待页面完全加载(避免动态内容未渲染完成)
  2. 定位最新10场赛事的条目容器
  3. 遍历每个赛事条目,通过胜负状态标签判断是否为曼联胜场
  4. 提取胜场的比分等信息

修改后的代码:

from bs4 import BeautifulSoup
from selenium import webdriver
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By

driver = webdriver.Chrome()
# 显式等待页面关键元素加载,比隐式等待更可靠
wait = WebDriverWait(driver, 10)
driver.get("https://www.livesport.com/en/team/manchester-united/ppjDR086/")

# 等待赛事列表加载完成
wait.until(EC.presence_of_element_located((By.CLASS_NAME, "wcl-rowRow-23T0_")))

page = driver.page_source
bs_obj = BeautifulSoup(page, "html.parser")

# 获取最新10场赛事条目
latest_matches = bs_obj.find_all("div", class_="wcl-rowRow-23T0_")[:10]

united_wins = []
for match in latest_matches:
    # 查找胜负状态标签(页面中会显示Win/Lose/Draw)
    status_tag = match.find("span", class_="wcl-matchStatus-2rO0_")
    if status_tag and status_tag.text.strip() == "Win":
        # 提取比分
        score = match.find("span", class_="wcl-simpleText_Asp-0 wcl-scores-simpleText-01_pV2Wk").text.strip()
        # 可选:提取对手和比赛时间
        opponent = match.find("span", class_="wcl-participantName-3MvRr").text.strip()
        match_time = match.find("div", class_="wcl-date-1l2P_").text.strip()
        united_wins.append({
            "对手": opponent,
            "比分": score,
            "时间": match_time
        })

# 输出胜场结果
for win in united_wins:
    print(f"{win['时间']} 曼联 {win['比分']} {win['对手']}")

driver.quit()

代码说明:

  • 使用WebDriverWait显式等待,确保赛事列表完全加载,避免因动态渲染导致元素无法找到
  • 通过wcl-rowRow-23T0_类定位单场赛事的容器,取前10个即为最新10场
  • 利用赛事条目中的wcl-matchStatus-2rO0_标签判断胜负,该标签会明确显示"Win"标识曼联胜场
  • 额外提取了对手和比赛时间,可根据需求调整提取的信息字段

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.13 09:06:17