【问题标题】:Finding all tags and attributes in a HTML查找 HTML 中的所有标签和属性
【发布时间】:2015-08-23 10:24:18
【问题描述】:

我是新手,第一次看 HTML 代码。对于我的研究,我需要知道网页中标签和属性的数量。

我查看了各种解析器,发现 Beautiful Soup 是最受欢迎的解析器之一。以下代码(取自Parsing HTML using Python)显示了解析文件的方式:

import urllib2
from BeautifulSoup import BeautifulSoup

page = urllib2.urlopen('http://www.google.com/')
soup = BeautifulSoup(page)

x = soup.body.find('div', attrs={'class' : 'container'}).text

我发现 find_all 非常有用,但需要一个参数才能找到一些东西。

有人可以指导我如何知道 html 页面中所有标签和属性的计数吗?

谷歌开发者工具可以在这方面提供帮助吗?

【问题讨论】:

    标签: python html xml-parsing beautifulsoup html-parsing


    【解决方案1】:

    如果您不带任何参数调用find_all(),它将递归查找页面上的所有元素。演示:

    >>> from bs4 import BeautifulSoup
    >>> 
    >>> data = """
    ... <html><head><title>The Dormouse's story</title></head>
    ... <body>
    ... <p class="title"><b>The Dormouse's story</b></p>
    ... 
    ... <p class="story">Once upon a time there were three little sisters; and their names were
    ... <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    ... <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    ... <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    ... and they lived at the bottom of a well.</p>
    ... 
    ... <p class="story">...</p>
    ... """
    >>> 
    >>> soup = BeautifulSoup(data)
    >>> for tag in soup.find_all():
    ...     print tag.name
    ... 
    html
    head
    title
    body
    p
    b
    p
    a
    a
    a
    p
    

    Padraic 向您展示了如何通过 BeautifulSoup 计算元素和属性。除此之外,这里是如何对lxml.html做同样的事情:

    from lxml.html import fromstring
    
    root = fromstring(data)
    print int(root.xpath("count(//*)")) + int(root.xpath("count(//@*)"))
    

    作为奖励,我做了一个简单的基准测试,证明后一种方法要快得多(在我的机器上,使用我的设置并且没有指定 would make BeautifulSoup use lxml under-the-hood 等的解析器......很多事情都会影响结果,但无论如何):

    $ python -mtimeit -s'import test' 'test.count_bs()'
    1000 loops, best of 3: 618 usec per loop
    $ python -mtimeit -s'import test' 'test.count_lxml_html()'
    10000 loops, best of 3: 114 usec per loop
    

    其中test.py 包含:

    from bs4 import BeautifulSoup
    from lxml.html import fromstring
    
    data = """
    <html><head><title>The Dormouse's story</title></head>
    <body>
    <p class="title"><b>The Dormouse's story</b></p>
    
    <p class="story">Once upon a time there were three little sisters; and their names were
    <a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
    <a href="http://example.com/lacie" class="sister" id="link2">Lacie</a> and
    <a href="http://example.com/tillie" class="sister" id="link3">Tillie</a>;
    and they lived at the bottom of a well.</p>
    
    <p class="story">...</p>
    """
    
    def count_bs():
        return sum(len(ele.attrs) + 1 for ele in BeautifulSoup(data).find_all())
    
    
    def count_lxml_html():
        root = fromstring(data)
        return int(root.xpath("count(//*)")) + int(root.xpath("count(//@*)"))
    

    【讨论】:

    • 有没有办法也知道 DOM 节点。我目前在地址栏中使用 javascript:alert(document.getElementsByTagName('*').length) 来了解 DOM 节点。
    • @shingaridavesh 抱歉,“知道 DOM 节点”是什么意思?
    • 网页中的DOM节点数。
    • @shingaridavesh 不是 count_lxml_html() 函数返回的内容吗?..抱歉没有再次获得 :)
    • @shingaridavesh 我认为这可能是一个好问题,考虑单独发布它,以便更多人可以提供帮助。谢谢。
    【解决方案2】:

    如果你想要所有标签和属性的计数:

    sum(len(ele.attrs) + 1 for ele in BeautifulSoup(page).find_all())
    

    【讨论】:

      猜你喜欢
      • 2013-11-14
      • 2011-04-16
      • 2011-03-02
      • 2021-06-12
      • 1970-01-01
      • 1970-01-01
      • 2013-05-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多