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

Python Selenium脚本仅获取每隔一个投注类型元素的问题求助

解决Selenium脚本漏抓投注类型的问题

你的问题出在每个投注组(OddGroups-styles-event-details-odds-groups)下包含多个投注类型行,但你用了单数的find_element方法,只会抓取每个组里的第一个投注行,自然漏掉了Double Chance这类同组内的其他选项。

修改方案很简单,把获取投注行的方法从单数改为复数,再嵌套循环遍历所有行:

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

driver = webdriver.Chrome()

driver.get('https://sports.tipico.de/en/event/552146710?t=match')
# 处理cookie弹窗
WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.CSS_SELECTOR, '#_evidon-accept-button'))).click()

# 显式等待投注区域加载完成,替代time.sleep更可靠
WebDriverWait(driver, 20).until(EC.presence_of_all_elements_located((By.CLASS_NAME, 'OddsRow-styles-odds-row')))

# 遍历每个投注组
for odds_group in driver.find_elements(By.XPATH, "//div[@class='OddGroups-styles-event-details-odds-groups']"):
    # 获取当前组下的所有投注行(改用find_elements复数方法)
    odds_rows = odds_group.find_elements(By.CLASS_NAME, 'OddsRow-styles-odds-row')
    # 遍历每一行提取投注类型
    for odds_row in odds_rows:
        try:
            bet_type = odds_row.find_element(By.CLASS_NAME, 'OddsCaption-styles-caption-cell-title').text
            if bet_type:  # 过滤空文本
                print(bet_type)
        except Exception as e:
            # 跳过可能加载异常的行
            continue

driver.quit()

修改说明:

  • 把find_element改为find_elements,确保抓取到每个组内的所有投注行
  • 用WebDriverWait等待所有投注行加载完成,比固定time.sleep更适配不同网络速度
  • 增加异常捕获,避免个别元素加载失败导致脚本中断
  • 过滤空文本,避免输出无效内容

运行修改后的脚本,就能抓取到包括Double Chance在内的全部投注类型了。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.08 13:31:08