网页爬取遇AttributeError:'NoneType'对象无'text'属性求助
Hey there! That AttributeError: 'NoneType' object has no attribute 'text' error is super common in web scraping—let's break down why it's happening and fix it right up.
Why You're Seeing This Error
When you use result.find(...) to look for an element, if BeautifulSoup can't find that element in the current div.det container, it returns None. Trying to call .text on None throws that error. This means some of the schedule containers on the page don't have one or more of the elements you're trying to extract (like the score link, maybe for upcoming games that don't have a score yet).
How to Fix It
You need to check if each element exists before trying to access its .text property. Here are two solid approaches:
1. Existence Checks with Default Values
This method lets you handle missing elements gracefully and assign fallback text for cases where an element isn't present:
import requests from bs4 import BeautifulSoup as soup my_url = requests.get('http://www.vivoelfutbol.com.mx/futbolmexicano.php') page_soup = soup(my_url.text, "html.parser") containers = page_soup.findAll("div", {"class":"det"}) for result in containers: # Extract time (with fallback if missing) time_element = result.find('div', {'class':'hor'}) match_time = time_element.text.strip() if time_element else "Time not listed" # Extract home team hometeam_element = result.find('a') home_team = hometeam_element.text.strip() if hometeam_element else "Home team unknown" # Extract score (this is likely where your error was happening) score_element = result.find('a', {'title':'Analisis y Antecedentes'}) match_score = score_element.text.strip() if score_element else "No score available" # Extract away team (fill in your actual selector here) awayteam_element = result.find('div', {'class':'equi vis'}) # Corrected to match common page structure away_team = awayteam_element.text.strip() if awayteam_element else "Away team unknown" # Print or store the data print(f"[{match_time}] {home_team} {match_score} {away_team}")
2. Try-Except Block for Quick Error Handling
If you just want to skip any containers that have missing elements (instead of filling in defaults), use a try-except block to catch the AttributeError:
import requests from bs4 import BeautifulSoup as soup my_url = requests.get('http://www.vivoelfutbol.com.mx/futbolmexicano.php') page_soup = soup(my_url.text, "html.parser") containers = page_soup.findAll("div", {"class":"det"}) for result in containers: try: match_time = result.find('div', {'class':'hor'}).text.strip() home_team = result.find('a').text.strip() match_score = result.find('a', {'title':'Analisis y Antecedentes'}).text.strip() away_team = result.find('div', {'class':'equi vis'}).text.strip() print(f"[{match_time}] {home_team} {match_score} {away_team}") except AttributeError: print("Skipping a container: Missing one or more elements") continue
Pro Tip
Double-check the page structure using your browser's developer tools! Right-click on a schedule entry and inspect it to confirm that every div.det container actually has all the elements you're trying to scrape. Sometimes pages have variations (like past vs. upcoming games) that use different HTML structures.
内容的提问来源于stack exchange,提问作者Dominic

