【发布时间】:2017-03-25 20:08:39
【问题描述】:
如何统计html中开始和结束标签的个数
ya.html
<div class="side-article txt-article">
<p>
<strong>
</strong>
<a href="http://batam.tribunnews.com/tag/polres/" title="Polres">
</a>
<a href="http://batam.tribunnews.com/tag/bintan/" title="Bintan">
</a>
</p>
<p>
<br>
</p>
<p>
<a href="http://batam.tribunnews.com/tag/polres/" title="Polres">
</a>
</p>
<p>
<a href="http://batam.tribunnews.com/tag/polres/" title="Polres">
</a>
<a href="http://batam.tribunnews.com/tag/bintan/" title="Bintan">
</a>
</p>
<br>
我的代码
from bs4 import BeautifulSoup
soup = BeautifulSoup(open('ya.html'), "html.parser")
num_apperances_of_tag = len(soup.find_all())
print num_apperances_of_tag
输出
13
但这不是我想要的,因为我的代码将<p> </p> 计为一个,而我想分别计算开始和结束标记。
如何计算html中开始和结束标签的数量? 所以输出将是
23
谢谢
【问题讨论】:
标签: python html tags beautifulsoup findall