用python和BeautifulSoup从html中提取表数据

2024-10-01 09:39:05 发布

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

我是python和beauthoulsopu库的新手。我试过很多东西,但没有运气。在

我的html代码可以是:

<form method = "post" id="FORM1" name="FORM1">
<table cellpadding=0 cellspacing=1 border=0 align="center" bgcolor="#cccccc">
   <tr>

    <td class="producto"><b>Club</b><br>

       <input value="CLUB TENIS DE MESA PORTOBAIL" disabled class="txtmascaraform" type="TEXT" name="txtClub" size="60" maxlength="55">
     </td> 
   <tr>
    <td colspan="2" class="producto"><b>Nombre Equipo</b><br>
    <input value="C.T.M. PORTOBAIL" disabled class="txtmascaraform" type="TEXT" name="txtNomEqu" size="100" maxlength="80">
    </td>
   </tr>
   <tr>
     <td class="producto"><b>Telefono fijo</b><br>
       <input value="63097005534" disabled class="txtmascaraform" type="TEXT" name="txtTelf" size="15" maxlength="10">
     </td

我只需要获取<;“b”>;“/b”>;及其“输入值”中的值。在

非常感谢!!在


Tags: textnamebrinputsizevaluetypetr
1条回答
网友
1楼 · 发布于 2024-10-01 09:39:05

首先按id find()输入表单,然后find_all()在里面输入并得到valueattribute的值:

from bs4 import BeautifulSoup


data = """<form method = "post" id="FORM1" name="FORM1">
<table cellpadding=0 cellspacing=1 border=0 align="center" bgcolor="#cccccc">
   <tr>

    <td class="producto"><b>Club</b><br>

       <input value="CLUB TENIS DE MESA PORTOBAIL" disabled class="txtmascaraform" type="TEXT" name="txtClub" size="60" maxlength="55">
     </td>
   <tr>
    <td colspan="2" class="producto"><b>Nombre Equipo</b><br>
    <input value="C.T.M. PORTOBAIL" disabled class="txtmascaraform" type="TEXT" name="txtNomEqu" size="100" maxlength="80">
    </td>
   </tr>
   <tr>
     <td class="producto"><b>Telefono fijo</b><br>
       <input value="63097005534" disabled class="txtmascaraform" type="TEXT" name="txtTelf" size="15" maxlength="10">
     </td>
   </tr>
</table>
</form>"""

soup = BeautifulSoup(data)
form = soup.find("form", {'id': "FORM1"})
print [item.get('value') for item in form.find_all('input')]

# UPDATE for getting table cell values
table = form.find("table")
print [item.text.strip() for item in table.find_all('td')]

印刷品:

^{pr2}$

相关问题 更多 >