简体   繁体   English

'NoneType' 对象没有属性 'text' | 美汤

[英]'NoneType' object has no attribute 'text' | Beautifulsoup

I've just started learning python webscraping and I wanted to learn how to scrape the data from the NFL Website to show all the players and their stats, but I'm given this error with the Beautifulsoup.我刚刚开始学习 python webscraping,我想学习如何从 NFL 网站抓取数据以显示所有球员及其统计数据,但我在 Beautifulsoup 中出现了这个错误。

import requests
from bs4 import BeautifulSoup

url = "https://www.pro-football-reference.com/years/2021/passing.htm"

r = requests.get(url)
soup = BeautifulSoup(r.text, 'html.parser')

league_table = soup.find('table', class_ = 'per_match_toggle sortable stats_table')

for name in league_table.find_all('tbody'):
    rows = name.find_all('tr')
    for row in rows:
        name = row.find('td', class_ = 'left').text.strip()
        yards = row.find_all('td', class_ = 'right')[7].text
        touchdowns = row.find_all('td', class_ = 'right')[8].text
        print("Name " + name + " Yards " + yards +  " Touchdowns " + touchdowns)

Error:错误:

name = row.find('td', class_ = 'left').text.strip()

This happens since find() can return None which, obviously, doesn't have an attribute of text .发生这种情况是因为find()可以返回None ,显然,它没有text属性。

This can happen when the element you are searching for doesn't exist or your passing the wrong argument to the search function.当您要搜索的元素不存在或您将错误的参数传递给搜索函数时,就会发生这种情况。

You should wrap the problematic section with a try-except clause or with an if else in order to deal with such scenarios您应该用try-except子句或if else包装有问题的部分,以处理此类情况

This happens because you will notice right after James Winston, there's a row of headers.发生这种情况是因为您会注意到在 James Winston 之后,有一排标题。 So that <tr> tag is made up of <th> tags, not <td> tags.所以<tr>标签由<th>标签组成,而不是<td>标签。 So it gets to that row, and you say .find('td') , which it does not contain so it returns None .所以它到达那一行,你说.find('td') ,它不包含所以它返回None Then you want to get the text from that, which you get get .text from None .然后你想从中获取文本,你从None得到.text

So you'll need to either, like the previous post suggested, utilize a try/except or logic that only takes rows with <td> tags.因此,您需要像上一篇文章所建议的那样,使用 try/except 或仅采用带有<td>标签的行的逻辑。

Personally, I'd just use pandas to grab the table, remove that header rows, and iterate through those rows.就个人而言,我只是使用 Pandas 来抓取表格,删除该标题行,然后遍历这些行。

import pandas as pd

url = "https://www.pro-football-reference.com/years/2021/passing.htm"
df = pd.read_html(url)[0]
df = df[df['Player'].ne('Player')]

for idx, row in df.iterrows():
    name = row['Player']
    yards = row['Yds']
    touchdowns = row['TD']
    print("Name " + name + " Yards " + yards +  " Touchdowns " + touchdowns)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 BeautifulSoup“AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39;” - BeautifulSoup "AttributeError: 'NoneType' object has no attribute 'text'" BeautifulSoup AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - BeautifulSoup AttributeError: 'NoneType' object has no attribute 'text' AttributeError:“NoneType”对象没有属性“文本”-Beautifulsoup - AttributeError: 'NoneType' object has no attribute 'text' - Beautifulsoup BeautifulSoup Python NoneType object 没有属性“文本” - BeautifulSoup Python NoneType object has no attribute 'text' &#39;NoneType&#39; 对象在 BeautifulSoup 中没有属性 &#39;text&#39; - 'NoneType' object has no attribute 'text' in BeautifulSoup BeautifulSoup: AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; - BeautifulSoup: AttributeError: 'NoneType' object has no attribute 'text' 'NoneType' object 没有属性 'text' BeautifulSoup Python - 'NoneType' object has no attribute 'text' BeautifulSoup Python AttributeError: &#39;NoneType&#39; 对象没有属性 &#39;text&#39; BeautifulSoup - AttributeError: 'NoneType' object has no attribute 'text' BeautifulSoup AttributeError: 'NoneType' object 在使用 BeautifulSoup 时没有属性 'text' - AttributeError: 'NoneType' object has no attribute 'text' when using BeautifulSoup AttributeError:&#39;NoneType&#39;对象没有属性&#39;text&#39;-Python,BeautifulSoup错误 - AttributeError: 'NoneType' object has no attribute 'text' - Python , BeautifulSoup Error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM