我想在html代码中找到一个特定的标签,比如如果有2个标签,那么我怎样才能获取第二个标签的内容而不是第一个标签的内容,soup.find(id='contact1')在这里是示例html代码
<table align="center"><th id="contact">STUDENT ID</th><th id="contact">NAME</th><th id="contact"> Phone </th><th id="contact"> NO.</th>
<p align="center" style="display:compact; font-size:18px; font-family:Arial, Helvetica, sans-serif; color:#CC3300">
</p><tr>
<td id="contact1">
2011XXA4438F </td> <td id="contact1"> SAM SRINIVAS KRISHNAGOPAL</td> <td id="contact1"> 9894398690 </td> <td id="contact1"> </td>
</tr>
</table>
我想要做的是将“2011XXA4438F”提取为字符串,我该怎么做?
<td id="contact1">
is the firstid 为的标签"contact1"
。要想获得它,那么soup.find
是你所需要的全部:
>>> print soup.find(id='contact1').text.strip()
2011XXA4438F
如果您正在寻找其他标签,那么您将需要使用find_all
:
>>> print soup.find_all(id='contact1')
[<td id="contact1">
2011XXA4438F </td>, <td id="contact1"> SAM SRINIVAS KRISHNAGOPAL</td>, <td id="contact1"> 9894398690 </td>, <td id="contact1"> </td>]
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)