-1

我正在尝试从表中找到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' )
4

1 回答 1

2

这意味着statement在无。所以可能这一行:soup.find ( 'table' , { 'class' : "financialStatement" })没有找到任何东西并返回None。

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

if statement:
   rows = statement.findAll ( 'tr' )
else:
   rows = None
于 2013-03-13T21:08:51.847 回答