使用BeautifulSoup爬取NFL伤病页面遇KeyError,需优化无伤病逻辑
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"]withinjury = tr.td.get("data-tip", "NA"): This line tries to fetch thedata-tipattribute; if it doesn't exist, it automatically uses "NA" as the fallback value. No more KeyError stopping your loop! - Renamed
playertoplayersfor 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

