Python 如何解决使用beautifulsoup时的属性错误?

Python 如何解决使用beautifulsoup时的属性错误?,python,dataframe,beautifulsoup,attributeerror,findall,Python,Dataframe,Beautifulsoup,Attributeerror,Findall,我正在读取一个.html文件,该文件的格式类似于以下格式: html = ''' <tr> <td class="SmallFormText" colspan="3">hours per response:</td><td class="SmallFormTextR">23.8</td> </tr> <hr> <table width="100%" border="0" cellspacing="0" ce

我正在读取一个.html文件,该文件的格式类似于以下格式:

html = '''
<tr>
<td class="SmallFormText" colspan="3">hours per response:</td><td class="SmallFormTextR">23.8</td>
</tr>
<hr>
<table width="100%" border="0" cellspacing="0" cellpadding="4" summary="Form 13F-NT Header Information">
<tbody>
<tr>
<td class="FormTextC">COLUMN 1</td><td class="FormTextC">COLUMN 2</td><td class="FormTextC">COLUMN 3</td><td class="FormTextR">COLUMN 4</td><td class="FormTextC" colspan="3">COLUMN 5</td><td class="FormTextC">COLUMN 6</td><td class="FormTextR">COLUMN 7</td><td class="FormTextC" colspan="3">COLUMN 8</td>
</tr>
<tr>
<td class="FormText"></td><td class="FormText"></td><td class="FormText"></td><td class="FormTextR">VALUE</td><td class="FormTextR">SHRS OR</td><td class="FormText">SH/</td><td class="FormText">PUT/</td><td class="FormText">INVESTMENT</td><td class="FormTextR">OTHER</td><td class="FormTextC" colspan="3">VOTING AUTHORITY</td>
</tr>
<tr>
<td class="FormText">NAME OF ISSUER</td><td class="FormText">TITLE OF CLASS</td><td class="FormText">CUSIP</td><td class="FormTextR">(x$1000)</td><td class="FormTextR">PRN AMT</td><td class="FormText">PRN</td><td class="FormText">CALL</td><td class="FormText">DISCRETION</td><td class="FormTextR">MANAGER</td><td class="FormTextR">SOLE</td><td class="FormTextR">SHARED</td><td class="FormTextR">NONE</td>
</tr>
<tr>
<td class="FormData">1ST SOURCE CORP</td><td class="FormData">COM</td><td class="FormData">336901103</td><td class="FormDataR">8</td><td class="FormDataR">335</td><td class="FormData">SH</td><td>&nbsp;</td><td class="FormData">SOLE</td><td class="FormData">7</td><td class="FormDataR">335</td><td class="FormDataR">0</td><td class="FormDataR">0</td>
</tr>
<tr>
<td class="FormData">1ST UNITED BANCORP INC FLA</td><td class="FormData">COM</td><td class="FormData">33740N105</td><td class="FormDataR">7</td><td class="FormDataR">989</td><td class="FormData">SH</td><td>&nbsp;</td><td class="FormData">SOLE</td><td class="FormData">7</td><td class="FormDataR">989</td><td class="FormDataR">0</td><td class="FormDataR">0</td>
</tr>    '''
在定义position之后,我得到了一个“AttributeError”,表示list对象没有属性“find_all”

这到底意味着什么?另外,我需要如何转换html数据以避免此问题

编辑部分:

以下是完整的堆栈跟踪:

position = rows.find_all('td')
Traceback (most recent call last):

  File "<ipython-input-8-37353b5ab2ef>", line 1, in <module>
    position = rows.find_all('td')

AttributeError: 'list' object has no attribute 'find_all'
position=rows.find_all('td'))
回溯(最近一次呼叫最后一次):
文件“”,第1行,在
位置=行。查找所有('td'))
AttributeError:“列表”对象没有“全部查找”属性

soup.find_all
返回元素的python
列表。您所需要做的就是遍历列表并从这些元素中获取数据

from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
rows = soup.find_all('tr')

# scan for header row and trim list
for index, row in enumerate(rows):
    cells = row.find_all('td')
    if cells and "NAME OF ISSUER" in cells[0].text.upper():
        del rows[:index+1]
        break

# convert remaining html rows to dict to create dataframe
positions = []
for position in rows:
    dic = {}
    cells = position.find_all('td')
    dic["NAME_OF_ISSUER"] = cells[0].text
    dic["CUSIP"] = cells[2].text
    dic["VALUE"] = int(cells[3].text.replace(',', ''))*1000
    dic["SHARES"] = int(celss[4].text.replace(',', ''))
    positions.append(dic)
df = pd.DataFrame(positions)

你能发布完整的堆栈跟踪吗?它向我们展示了这行代码,以及可能出现问题的属性。
soup.find_all
提供了python
元素列表。列表没有
find_all
方法。您可能需要一个
for
循环来遍历这些元素,并对它们执行
查找所有的
。谢谢您的帮助回复,@tdelaney。我已经在编辑的部分下面发布了跟踪。我明白了,但即使我在行上循环,我仍然会得到相同的错误。我将尝试一下您的代码。我刚刚尝试使用发布的示例(即示例html文件)运行此代码,但df变量似乎为空,@tdelaney。换句话说,DataFram的大小是(0,0)。这可能是由于数据格式错误造成的吗?换句话说,当我将原始XML文件(来自SEC 13-F文件网页)保存为HTML文件时,这是否可能导致df变量为空?只要手动查看html变量(在原始文章中提供),就会有许多“tr”语句。所以我不确定为什么df变量是空的。我有一个bug。我在空位置列表而不是行中进行交互。我还更新了扫描标题行,而不是仅仅假设它位于索引10。非常感谢您的有用评论。我非常感谢您的时间和支持!
from bs4 import BeautifulSoup
soup = BeautifulSoup(html, 'lxml')
rows = soup.find_all('tr')

# scan for header row and trim list
for index, row in enumerate(rows):
    cells = row.find_all('td')
    if cells and "NAME OF ISSUER" in cells[0].text.upper():
        del rows[:index+1]
        break

# convert remaining html rows to dict to create dataframe
positions = []
for position in rows:
    dic = {}
    cells = position.find_all('td')
    dic["NAME_OF_ISSUER"] = cells[0].text
    dic["CUSIP"] = cells[2].text
    dic["VALUE"] = int(cells[3].text.replace(',', ''))*1000
    dic["SHARES"] = int(celss[4].text.replace(',', ''))
    positions.append(dic)
df = pd.DataFrame(positions)