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

如何从HTML标签提取data-odd属性值?竞彩赔率爬取技术问题

Extracting data-odd Values from Nested and Direct TD Attributes

Hey there! Let's tackle this odds extraction problem step by step. The key issue here is that your data-odd values are stored in two different places across those TD elements—some directly on the TD itself, others nested inside a span. Here's how to grab all three values cleanly:

First, Understand the Structure Differences

Let's recap the two scenarios you're dealing with:

  • Row 3 & 4: The data-odd is a direct attribute of the <td> tag
  • Row 2: The data-odd lives on an inner <span> tag nested inside the TD

Solution 1: Target Each Case Explicitly

If you want to handle each TD individually, use these code snippets:

# For row[3] and row[4] (data-odd is on the TD itself)
odd_3 = row[3].get('data-odd')  # Returns "3.18"
odd_4 = row[4].get('data-odd')  # Returns "4.75"

# For row[2] (data-odd is on a nested span)
# Find the span with a data-odd attribute inside the TD
target_span = row[2].find('span', attrs={'data-odd': True})
odd_2 = target_span.get('data-odd') if target_span else None  # Returns "1.73"

Solution 2: Create a Reusable Function

To make this scalable (in case more rows mix these two structures), write a helper function that handles both scenarios automatically:

def get_odd_value(td_element):
    # First check if the TD itself has the data-odd attribute
    direct_odd = td_element.get('data-odd')
    if direct_odd:
        return direct_odd
    
    # If not, look for a nested span with data-odd
    nested_span = td_element.find('span', {'data-odd': True})
    return nested_span.get('data-odd') if nested_span else None

# Now extract all three odds with one function call
odd_2 = get_odd_value(row[2])
odd_3 = get_odd_value(row[3])
odd_4 = get_odd_value(row[4])

# Output will be: 1.73 3.18 4.75
print(odd_2, odd_3, odd_4)

Why Your Initial Approach Didn't Work

When you tried accessing the TD directly for row[2], the data-odd attribute isn't present on the TD tag—it's buried inside three layers of spans. The find() method lets you drill down to that inner span, and get() safely retrieves the attribute value (without throwing an error if the element is missing).

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:20:43