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

如何使用Python中的BeautifulSoup遍历具有不同标识符的标记,python,tags,beautifulsoup,Python,Tags,Beautifulsoup,这可能是一个简单的问题,但我想遍历id=dgrdAcquired\u hyplnkacquired\u 0、dgrdAcquired\u hyplnkacquired\u 1等的标记 有没有比下面的代码更简单的方法?问题是,这些标签的数量对于我打开的每个网页都是不同的。当每个网页可能有不同数量的标签时,我不确定如何获取这些标签中的文本 html = """ <tr> <td colspan="3"><table class="datagrid" cellspacin

这可能是一个简单的问题,但我想遍历id=dgrdAcquired\u hyplnkacquired\u 0、dgrdAcquired\u hyplnkacquired\u 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
html=”“”
名称(RSSD ID)采集日期描述
2011-04-30
被收购机构失败,处置由监管机构安排,资产分配给收购机构。
"""
soup=BeautifulSoup(html)
firm1=soup.find('a',{“id”:“dgrdAcquired\u hyplnkacquired\u 0”})
data1=''.join(firm1.findAll(text=True))
打印数据1
firm2=soup.find('a',{“id”:“dgrdAcquired\u hyplnkacquired\u 1})
data2=''.join(firm2.findAll(text=True))
打印数据2

我将执行以下操作,假设有
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

在这种特殊情况下,正则表达式可能有些过分。
尽管如此,这里还有另一个选择:

import re
soup.find_all('a', id=re.compile(r'[dgrdAcquired_hyplnkacquired_]\d+'))
请注意:
s/find_all/findAll/g
如果使用BS3。
结果(为了显示,删除了一点空白):

[,,
]
[<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>]