gpt4 book ai didi

python - 查找 HTML 中的所有标签和属性

转载 作者:太空宇宙 更新时间:2023-11-04 15:03:40 27 4
gpt4 key购买 nike

我是新手,第一次看 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 页面中所有标记和属性的数量

Google 开发者工具在这方面有帮助吗?

最佳答案

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

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

关于python - 查找 HTML 中的所有标签和属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30717853/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com