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

使用BeautifulSoup爬取NFL伤病页面遇KeyError,需优化无伤病逻辑

Fixing KeyError When Scraping Injury Data with BeautifulSoup

Hey there, let's sort out that KeyError you're hitting while scraping the Pro Football Reference injury page. The issue is straightforward: when a <td> tag doesn't have the data-tip attribute, trying to access it directly with tr.td["data-tip"] throws a KeyError—your if-else check never even gets to run because the error happens before that line.

The Fix: Use .get() for Safe Attribute Access

BeautifulSoup's Tag object has a .get() method that lets you safely retrieve attributes without triggering errors. If the attribute doesn't exist, it returns None (or a default value you specify), which lets you handle missing data cleanly. Here's your revised code:

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://www.pro-football-reference.com/teams/nwe/2013_injuries.htm'
# Opening connection and grabbing the page
uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

# HTML parsing
page_soup = soup(page_html, "html.parser")
containers = page_soup.find("tbody")
players = containers.find_all("tr")

for tr in players:
    # Extract player name from <th> tags
    name = [th.text for th in tr.find_all("th")]
    # Get week number (data-stat is guaranteed to exist)
    week = tr.td["data-stat"]
    # Safely get injury status: use "NA" if data-tip doesn't exist
    injury = tr.td.get("data-tip", "NA")
    # Print the result in your desired format
    print([name, injury, week])

What Changed?

  • Replaced injury = tr.td["data-tip"] with injury = tr.td.get("data-tip", "NA"): This line tries to fetch the data-tip attribute; if it doesn't exist, it automatically uses "NA" as the fallback value. No more KeyError stopping your loop!
  • Renamed player to players for better readability (since we're working with a list of player rows).

Example Output

Running this code will produce exactly the output you want, including all players without interruption:

[['Danny Amendola'], 'Questionable: hamstring', 'week_1']
[['Armond Armstead'], 'Out: infection', 'week_1']
[['Kyle Arrington'], 'NA', 'week_1']
# ... all remaining players will be printed correctly

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:13:23