【问题标题】:python how to count the number of the opening and closing tags in htmlpython如何计算html中开始和结束标签的数量
【发布时间】: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

但这不是我想要的,因为我的代码将&lt;p&gt; &lt;/p&gt; 计为一个,而我想分别计算开始和结束标记。

如何计算html中开始和结束标签的数量? 所以输出将是

23 

谢谢

【问题讨论】:

    标签: python html tags beautifulsoup findall


    【解决方案1】:

    我建议你使用 html 解析器来解决这个问题:

    from HTMLParser import HTMLParser
    
    number_of_starttags = 0
    number_of_endtags = 0
    
    # create a subclass and override the handler methods
    class MyHTMLParser(HTMLParser):
        def handle_starttag(self, tag, attrs):
            global number_of_starttags
            number_of_starttags += 1
    
        def handle_endtag(self, tag):
            global number_of_endtags
            number_of_endtags += 1
    
    # instantiate the parser and fed it some HTML
    parser = MyHTMLParser()
    parser.feed('<html><head><title>Test</title></head><body><h1>Parse me!</h1></body></html>')
    
    print(number_of_starttags, number_of_endtags)
    

    【讨论】:

    • 它对我不起作用我得到 UnboundLocalError: local variable 'number_of_starttags' referenced before assignment.
    • 对,因为班级。只需为变量指明全局即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 2016-02-12
    • 1970-01-01
    • 1970-01-01
    • 2014-07-21
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多