Python错误:"NoneType"对象没有属性"find_all" [英] Python error: 'NoneType' object has no attribute 'find_all'

查看:165
本文介绍了Python错误:"NoneType"对象没有属性"find_all"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从以下网站改编Web抓取程序: http://danielfrg.com/blog/2013/04/01/nba-scraping-data/#disqus_thread ,将棒球数据的ESPN抓取为CSV.但是,当我运行第二段代码来编写csv游戏时,从下面的代码部分

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

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.

完整的错误文本为

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.

推荐答案

该错误表示您正在执行以下操作来构建 table 变量:

The error means that the table variable that you are building by doing:

table = BeautifulSoup(r.text).table

返回 None .在 None 上的table.find_all("tr")[1:]:中的行的引发错误.

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

您可以检查所涉及的 url 是否具有试图访问该表的方式的表.您可以通过打印以下语句构造的 url 来做到这一点:

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

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

,然后在浏览器中转到该URL,以检查它是否具有您感兴趣的表.

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

这篇关于Python错误:"NoneType"对象没有属性"find_all"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆