Python 错误:“NoneType”对象没有属性“find_all”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23186484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:26:37  来源:igfitidea点击:

Python error: 'NoneType' object has no attribute 'find_all'

pythonobjectattributeerrornonetype

提问by user3554610

I'm adapting a web scraping program from, http://danielfrg.com/blog/2013/04/01/nba-scraping-data/#disqus_thread, to scrape ESPN for baseball data into a CSV. However when I run the second piece of code to write a csv of games I get the 'NoneType' object has no attribute 'find_all' error, from the following section of code

我正在改编来自http://danielfrg.com/blog/2013/04/01/nba-scraping-data/#disqus_thread的网络抓取程序,以将 ESPN 的棒球数据抓取到 CSV 中。但是,当我运行第二段代码来编写一个 csv 游戏时,我从以下代码部分得到“NoneType”对象没有属性“find_all”错误

for index, row in teams.iterrows():
    _team, url = row['team'], row['url']
    r = requests.get(BASE_URL.format(row['prefix_1'], year, row['prefix_2']))
    table = BeautifulSoup(r.text).table
    for row in table.find_all("tr")[1:]: # Remove header
        columns = row.find_all('td')
        try:
            _home = True if columns[1].li.text == 'vs' else False
            _other_team = columns[1].find_all('a')[1].text
            _score = columns[2].a.text.split(' ')[0].split('-')
            _won = True if columns[2].span.text == 'W' else False

            match_id.append(columns[2].a['href'].split('?id=')[1])
            home_team.append(_team if _home else _other_team)
            visit_team.append(_team if not _home else _other_team)
            d = datetime.strptime(columns[0].text, '%a, %b %d')
            dates.append(date(year, d.month, d.day))

I can post the whole program but this is the piece of code the compiler reads the error for.

我可以发布整个程序,但这是编译器读取错误的代码段。

The full error text is

完整的错误文本是

Traceback (most recent call last):
  File "C:\Python27\Project Files\Game Parser.py", line 23, in <module>
    for row in table.find_all("tr")[1:]: # Remove header
AttributeError: 'NoneType' object has no attribute 'find_all'

Any help on how to get this code running would be greatly appreciated.

任何有关如何运行此代码的帮助将不胜感激。

采纳答案by shaktimaan

The error means that the tablevariable that you are building by doing:

该错误意味着table您正在通过执行以下操作来构建变量:

table = BeautifulSoup(r.text).table

is returning None. And for row in table.find_all("tr")[1:]:on a Noneis throwing the error.

正在返回None。并且for row in table.find_all("tr")[1:]:在 aNone上抛出错误。

You can check if the urlin question has a table in the way you are trying to access it. You can do this by printing out the urlconstructed by this statement:

您可以检查有url问题的表格是否以您尝试访问的方式存在。您可以通过打印出url此语句构造的来实现此目的:

BASE_URL.format(row['prefix_1'], year, row['prefix_2'])

and then going to this url in your browser to check if it has the table of your interest.

然后在浏览器中访问此 url 以检查它是否包含您感兴趣的表格。