如何使用Python中的BeautifulSoup迭代具有不同标识符的标记

时间:2012-03-27 18:04:22

标签: python tags beautifulsoup

这可能是一个简单的问题,但我想用id = dgrdAcquired_hyplnkacquired_0,dgrdAcquired_hyplnkacquired_1等来迭代这些标签。

有没有比我下面的代码更简单的方法呢?问题是我拉出的每个网页的这些标签的数量会有所不同。我不确定当每个网页可能有不同数量的标签时,如何在这些标签中获取文字。

html = """
<tr>
<td colspan="3"><table class="datagrid" cellspacing="0" cellpadding="3" rules="rows" id="dgrdAcquired" width="100%">
<tr class="datagridH">
<th scope="col"><font face="Arial" color="Blue" size="2"><b>Name (RSSD ID)</b></font></th><th scope="col"><font face="Arial" color="Blue" size="2"><b>Acquisition Date</b></font></th><th scope="col"><font face="Arial" color="Blue" size="2"><b>Description</b></font></th>
</tr><tr class="datagridI">
<td nowrap="nowrap"><font face="Arial" size="2">
<a id="dgrdAcquired_hyplnkacquired_0" href="InstitutionProfile.aspx?parID_RSSD=3557617&parDT_END=20110429">FIRST CHOICE COMMUNITY BANK                                                                                              (3557617)</a>
</font></td><td><font face="Arial" size="2">
<span id="dgrdAcquired_lbldtAcquired_0">2011-04-30</span>
</font></td><td><font face="Arial" size="2">
<span id="dgrdAcquired_lblAcquiredDescText_0">The acquired institution failed and disposition was arranged of by a regulatory agency.  Assets were distributed to the acquiring institution.</span>
</font></td>
</tr><tr class="datagridAI">
<td nowrap="nowrap"><font face="Arial" size="2">
<a id="dgrdAcquired_hyplnkacquired_1" href="InstitutionProfile.aspx?parID_RSSD=104038&parDT_END=20110429">PARK AVENUE BANK, THE                                                                                                    (104038)</a>
</font></td>
"""
soup = BeautifulSoup(html)
firm1 = soup.find('a', { "id" : "dgrdAcquired_hyplnkacquired_0"})
data1 = ''.join(firm1.findAll(text=True))
print data1

firm2 = soup.find('a', { "id" : "dgrdAcquired_hyplnkacquired_1"})
data2 = ''.join(firm2.findAll(text=True))
print data2

2 个答案:

答案 0 :(得分:1)

我会执行以下操作,假设有n个此类标记,则编号为0...n

soup = BeautifulSoup(html)
i = 0
data = []
while True:
    firm1 = soup.find('a', { "id" : "dgrdAcquired_hyplnkacquired_%s" % i})
    if not firm1:
        break
    data.append(''.join(firm1.findAll(text=True)))
    print data[-1]
    i += 1

答案 1 :(得分:1)

在这种特殊情况下,正则表达可能有点过分 尽管如此,这是另一种选择:

import re
soup.find_all('a', id=re.compile(r'[dgrdAcquired_hyplnkacquired_]\d+'))

如果使用BS3,请注意s/find_all/findAll/g 结果(为了显示的目的,删除了一些空格):

[<a href="InstitutionProfile.aspx?parID_RSSD=3557617&amp;parDT_END=20110429" 
  id="dgrdAcquired_hyplnkacquired_0">FIRST CHOICE COMMUNITY BANK (3557617)</a>,
 <a href="InstitutionProfile.aspx?parID_RSSD=104038&amp;parDT_END=20110429" 
  id="dgrdAcquired_hyplnkacquired_1">PARK AVENUE BANK, THE (104038)</a>]