1

大约一周前我遇到了一个问题。因为我认为这个解决方案很酷,所以我在这里分享它,同时等待我之前发布的问题的答案。我需要知道表中列标题的相对位置,以便知道如何将列标题与下面行中的数据匹配。我发现我的一些表有以下行作为表中的第一行

<!-- Table Width Row -->
<TR style="font-size: 1pt" valign="bottom">
<TD width="60%">&nbsp;</TD> <!-- colindex=01 type=maindata -->
<TD width="1%">&nbsp;</TD>  <!-- colindex=02 type=gutter -->
<TD width="1%" align="right">&nbsp;</TD>    <!-- colindex=02 type=lead -->
<TD width="9%" align="right">&nbsp;</TD>    <!-- colindex=02 type=body -->
<TD width="1%" align="left">&nbsp;</TD> <!-- colindex=02 type=hang1 -->

<TD width="3%">&nbsp;</TD>  <!-- colindex=03 type=gutter -->
<TD width="1%" align="right">&nbsp;</TD>    <!-- colindex=03 type=lead -->
<TD width="4%" align="right">&nbsp;</TD>    <!-- colindex=03 type=body -->
<TD width="1%" align="left">&nbsp;</TD> <!-- colindex=03 type=hang1 -->
<TD width="3%">&nbsp;</TD>  <!-- colindex=04 type=gutter -->
<TD width="1%" align="right">&nbsp;</TD>    <!-- colindex=04 type=lead -->

<TD width="4%" align="right">&nbsp;</TD>    <!-- colindex=04 type=body -->
<TD width="1%" align="left">&nbsp;</TD> <!-- colindex=04 type=hang1 -->
<TD width="3%">&nbsp;</TD>  <!-- colindex=05 type=gutter -->
<TD width="1%" align="right">&nbsp;</TD>    <!-- colindex=05 type=lead -->
<TD width="5%" align="right">&nbsp;</TD>    <!-- colindex=05 type=body -->
<TD width="1%" align="left">&nbsp;</TD> <!-- colindex=05 type=hang1 -->

 </TR>

我想哇,这很容易,因为数据在 type=body 下面的列中。倒计时我知道在数据行中我需要获取列 [3, 7, 11, 15] 中的值。所以我开始使用这段代码来完成它:

indexComment = souptoGetColIndex.findAll(text=re.compile("type=body"))
indexRow=indexComment[0].findParent()
indexCells=indexRow.findAll(text=re.compile("type=body"))
for each in range(len(indexCells)):
    collist.append(tdlist.index(indexCells[each].previousSibling.previousSibling))

我得到的是 collist=[0, 3, 7, 7, 15]

事实证明,我认为因为第 7 和第 11 位置的单元格看起来完全一样,所以返回了相同的索引位置。我试图弄清楚如何处理这个问题,显然我必须让它们看起来不同。所以我所做的是首先使用 readlines 读取文件的每一行并将空格更改为随机整数,从而使它们看起来不同。

for each in toGetColIndex:
   newlt.append(each.replace(r"&nbsp;",str(random.randint(1,14567))))

一位朋友指出,我可以通过使用它来降低开销

for each in toGetColIndex:
   newlt.append(each.replace(r"&nbsp;",str(toGetColIndex.index(each))))

尽管如此,这些方法中的每一种都为我提供了一个包含 colindex 的列表,用于我的每列标题的位置并在数据行上使用。请注意,替换功能缺少空格,因为我猜 html 导致它消失实际代码使用 r"&.nbsp;" 没有期间

4

1 回答 1

1

下面的代码产生 [3, 7, 11, 15] 这就是我理解你所寻求的

from BeautifulSoup import BeautifulSoup
from re import compile

soup = BeautifulSoup(
    '''<HTML><BODY>
    <TABLE>
    <TR style="font-size: 1pt" valign="bottom">
    <TD width="60%"> </TD> <!-- colindex=01 type=maindata -->
    <TD width="1%"> </TD>  <!-- colindex=02 type=gutter -->
    <TD width="1%" align="right"> </TD>    <!-- colindex=02 type=lead -->
    <TD width="9%" align="right"> </TD>    <!-- colindex=02 type=body -->
    <TD width="1%" align="left"> </TD> <!-- colindex=02 type=hang1 -->

    <TD width="3%"> </TD>  <!-- colindex=03 type=gutter -->
    <TD width="1%" align="right"> </TD>    <!-- colindex=03 type=lead -->
    <TD width="4%" align="right"> </TD>    <!-- colindex=03 type=body -->
    <TD width="1%" align="left"> </TD> <!-- colindex=03 type=hang1 -->
    <TD width="3%"> </TD>  <!-- colindex=04 type=gutter -->
    <TD width="1%" align="right"> </TD>    <!-- colindex=04 type=lead -->

    <TD width="4%" align="right"> </TD>    <!-- colindex=04 type=body -->
    <TD width="1%" align="left"> </TD> <!-- colindex=04 type=hang1 -->
    <TD width="3%"> </TD>  <!-- colindex=05 type=gutter -->
    <TD width="1%" align="right"> </TD>    <!-- colindex=05 type=lead -->
    <TD width="5%" align="right"> </TD>    <!-- colindex=05 type=body -->
    <TD width="1%" align="left"> </TD> <!-- colindex=05 type=hang1 -->

     </TR>
    </TABLE> </BODY></HTML>'''
)

tables = soup.findAll('table')
matcher = compile('colindex')

def body_cols(row):
    for i, comment in enumerate(row.findAll(text=matcher)):
        if 'type=body' in comment:
            yield i

for table in soup.findAll('table'):
    index_row = table.find('tr')
    print list(body_cols(index_row))
于 2008-10-20T16:13:14.957 回答