NoneType具有多个html文件的AttributeError

时间:2013-03-13 21:05:01

标签: python file web-scraping beautifulsoup nonetype

我正在尝试从tr表中找到financialStatement标记。一切正常,直到我编写最后一行rows = statement.findAll ( 'tr' ),这给出了:

AttributeError: 'NoneType' object has no attribute 'findAll'

这里是html文件:http://investing.businessweek.com/research/stocks/financials/financials.asp?ticker=TSCO:LN

import os
import urllib,re
from BeautifulSoup import BeautifulSoup 

path = '/Users/projectfile/'
listing = os.listdir(path)

for infile in listing:
    print "current file is: " + infile
    fileIN = open(path+infile, "r")
    line = fileIN.read()
    soup = BeautifulSoup ( line )
    statement = soup.find ( 'table' , { 'class' : "financialStatement" })
    rows = statement.findAll ( 'tr' )

1 个答案:

答案 0 :(得分:2)

这表示无statement。所以可能这一行:soup.find ( 'table' , { 'class' : "financialStatement" })找不到任何内容并返回None。

您可以添加if语句来测试if语句是否具有值:

if statement:
   rows = statement.findAll ( 'tr' )
else:
   rows = None