Beautifulsoup对于python中的html解析非常方便,下面的代码结果可以帮助我。属性“class”的优惠券返回列表,而其他属性的值为
from bs4 import BeautifulSoup
tr ="""
<table>
<tr class="passed" id="row1"><td>t1</td></tr>
<tr class="failed" id="row2"><td>t2</td></tr>
</table>
"""
table = BeautifulSoup(tr,"html.parser")
for row in table.findAll("tr"):
print row["class"]
print row["id"]
结果:
[u'passed']
row1
[u'failed']
row2
为什么属性class
收益为数组?而id
是正常值?
beautifulsoup4-4.5.0
与python 2.7
感谢快速回答,从@alecxe接受的答案,我注意到'class'是HTML和BS4一个特殊属性 –