在HTML中查找所有标签和属性 [英] Finding all tags and attributes in a HTML

查看:216
本文介绍了在HTML中查找所有标签和属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am a newbie and looking at HTML code for first time. For my research I need to know the number of tags and attributes in a webpage.

我查看了各种解析器,发现美丽汤"是最受欢迎的解析器之一.以下代码(取自使用Python解析HTML )显示了解析文件的方法:

I looked at various parser and found Beautiful Soup to be one of the most preferred one. The following code (taken from Parsing HTML using Python) shows the way to parse a file:

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非常有用,但是需要一个参数来查找某些东西.

I found find_all quite useful, but needs an argument to find something.

有人可以指导我如何了解html页面中的所有标记和属性的数量吗?

Can someone guide me on how to know the count of all tags and attributes in a html page?

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

Can google developer tool help in that regard?

推荐答案

如果您致电 find_all() ,不带任何参数,它将以递归方式查找页面上的所有元素.演示:

If you would call find_all() without any arguments, it would find all elements on a page recursively. Demo:

>>> 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 的方法:


Padraic showed you how to count elements and attributes via BeautifulSoup. In addition to it, here is how to do the same with lxml.html:

from lxml.html import fromstring

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

作为奖励,我做了一个简单的基准测试,证明后一种方法要快得多(在我的机器上,使用我的设置,并且没有指定解析器,该解析器

As a bonus, I've made a simple benchmark demonstrating that the latter approach is much faster (on my machine, with my setup and without specifying a parser which would make BeautifulSoup use lxml under-the-hood etc..a lot of things can affect the results, but anyway):

$ 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(//@*)"))

这篇关于在HTML中查找所有标签和属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆