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

Python爬取EuroLeague历史比赛URL遇动态页面问题求助

解决EuroLeague历史赛事URL爬取问题

问题根源

该网站属于单页应用(SPA),直接通过URL参数?round=8&season=E2022访问时,页面可能未触发历史赛事的动态数据加载;或是Selenium执行抓取操作时,页面还未完成历史赛事内容的渲染,导致你只能获取当前赛事的链接。

解决方案

1. 显式等待动态内容加载

直接调用driver.get()后立即抓取元素,大概率会碰到AJAX请求未完成、历史数据未渲染的情况。需要通过显式等待确保目标元素加载完成:

修改后的代码:

import pandas as pd 
from selenium import webdriver 
from selenium.webdriver import Chrome 
from selenium.webdriver.common.by import By 
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

driver = Chrome() 
url = "https://www.euroleaguebasketball.net/euroleague/game-center/?round=8&season=E2022" 
 
driver.get(url) 
# 最长等待10秒,直到至少一个赛事链接加载完成
wait = WebDriverWait(driver, 10)
wait.until(EC.presence_of_element_located((By.CSS_SELECTOR, "[class*=game-card-view_linkWrap__]")))

anchor_tags = driver.find_elements(By.CSS_SELECTOR, "[class*=game-card-view_linkWrap__]")
hrefs = [tag.get_attribute("href") for tag in anchor_tags]

print(hrefs)
driver.quit()

2. 手动触发赛季与轮次选择(应对URL参数不生效场景)

部分SPA会忽略URL参数,需要手动操作页面上的赛季/轮次选择器切换内容:

# 接上述driver.get(url)之后
# 等待赛季选择器可点击并展开
season_dropdown = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[class*=season-selector__trigger]")))
season_dropdown.click()
# 选择2022赛季(根据页面实际文本或value调整)
season_option = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[contains(text(), '2022-23')]")))
season_option.click()

# 等待轮次选择器可点击并展开
round_dropdown = wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "[class*=round-selector__trigger]")))
round_dropdown.click()
# 选择第8轮
round_option = wait.until(EC.element_to_be_clickable((By.XPATH, "//div[text()='Round 8']")))
round_option.click()

# 等待页面内容刷新后重新抓取
wait.until(EC.staleness_of(driver.find_elements(By.CSS_SELECTOR, "[class*=game-card-view_linkWrap__]")[0]))
anchor_tags = driver.find_elements(By.CSS_SELECTOR, "[class*=game-card-view_linkWrap__]")
hrefs = [tag.get_attribute("href") for tag in anchor_tags]

3. 禁用浏览器缓存避免旧内容干扰

浏览器缓存可能导致页面加载旧数据,启动Chrome时可添加禁用缓存参数:

from selenium.webdriver.chrome.options import Options

chrome_options = Options()
chrome_options.add_argument("--disable-cache")
chrome_options.add_argument("--disable-application-cache")

driver = Chrome(options=chrome_options)

验证要点

  • 运行代码时观察浏览器窗口,确认页面是否成功切换到2022赛季第8轮
  • 若CSS选择器失效,可改用XPATH直接定位目标赛事,比如//a[contains(@href, 'real-madrid-alba-berlin')]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 11:42:41