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一個特殊屬性 –