如何使用BeautifulSoup来获取colindex数?

2024-09-27 04:12:35 发布

您现在位置:Python中文网/ 问答频道 /正文

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

<!-- 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]中的值。因此,我开始使用以下代码来实现:

^{pr2}$

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

结果我认为,因为第7和第11位置的单元格看起来完全一样,所以返回了相同的索引位置。我在想办法解决这个问题,显然我得让它们看起来与众不同。所以我所做的是让他们看起来不同,首先使用一个读行读取文件中的每一行,并将空白空间更改为一个随机整数。在

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“& & .N.B.S.P”;没有句号


Tags: 数据righttypebodywidthlefttdeach
1条回答
网友
1楼 · 发布于 2024-09-27 04:12:35

下面的代码生成[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))

相关问题 更多 >

    热门问题