如何从BeautifulSoup表的TD中获取价值?

2022-02-21 00:00:00 python beautifulsoup

问题描述

我有一个页面,其源代码中包含一些表:

<table width='100%' cellspacing='0' cellpadding='2' class='an'>
    <tr>
        <td width='35%' align='right'>XXX :</td>
        <td><b>20</b></td>
    </tr>
    <tr><
        td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
</table>

<table width='361' cellspacing='0' cellpadding='2' class='an'>
    <tr>
        <td width='35%' align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XX :</td>
        <td><a href='XXX'><b>XXX</b></a></td>
    </tr>
    <tr>
        <td align='right'>PHONE :</td>
        <td><b>518878943</b></td>
    </tr>
</table>

我想从此页面获取第二个表中的电话号码:

<td align='right'>PHONE :</td>
<td><b>518878943</b></td>

但是,我的代码:

page_src="""<table width='100%' cellspacing='0' cellpadding='2' class='an'>
    <tr>
        <td width='35%' align='right'>XXX :</td>
        <td><b>20</b></td>
    </tr>
    <tr><
        td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
</table>

<table width='361' cellspacing='0' cellpadding='2' class='an'>
    <tr>
        <td width='35%' align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XXX :</td>
        <td><b>XXX</b></td>
    </tr>
    <tr>
        <td align='right'>XX :</td>
        <td><a href='XXX'><b>XXX</b></a></td>
    </tr>
    <tr>
        <td align='right'>PHONE :</td>
        <td><b>518878943</b></td>
    </tr>
</table>
"""

soup = BeautifulSoup(page_src, 'html.parser')
divs = soup.findAll("table", {"class": "an"})
for div in divs:
    row = ''
    rows = [row in div.findAll('tbody').findAll('tr')]

给我这样一条错误消息:

Traceback (most recent call last):
  File "test.py", line 198, in <module>
    rows = [row in div.findAll('tbody').findAll('tr')]
AttributeError: 'ResultSet' object has no attribute 'findAll'

如何解决此问题,并从页面获取电话号码?谢谢

编辑:

部分解决。部分原因是我认为我的解决方案很难看,但很管用。也许有人会想出更漂亮的解决方案?

tds = []
soup = BeautifulSoup(page_src, 'html.parser')
divs = soup.findAll("table", {"class": "an"})
for div in divs:
    rows = div.findAll('tr')
    for row in rows :
        tds.append(row.findAll('td'))
phone = str(tds[12][1])
phone = phone.replace("<td><b>", "").replace("</b></td>", "").strip()
print phone

解决方案

找到包含PHONE :td元素,然后获取following sibling element。一行:

soup.find("td", text="PHONE :").find_next_sibling("td").text

相关文章