gpt4 book ai didi

python - 在python中使用beautifulsoup提取网页的数据丰富节点

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

在python中使用beautifulsoup提取网页的数据丰富的节点,有没有办法统计标签在页面中出现的频率,

import requests
from bs4 import BeautifulSoup

url = "http://www.amazon.in"
r = requests.get(url)

html = BeautifulSoup(r.content)

现在我想统计获取到的html中所有标签的频率。

最佳答案

使用dict comprehensionscollection.Counter获取作为 bs4.element.Tag 实例的 tags 的计数。

from collections import Counter
import requests
import bs4
from bs4 import BeautifulSoup
url = "http://www.amazon.in"
r = requests.get(url)
html = BeautifulSoup(r.content)
Counter(tag.name for tag in html.descendants if isinstance(tag, bs4.element.Tag))

输出

Counter({'div': 462, 'a': 448, 'span': 395, 'li': 288, 'br': 78, 'img': 60, 'td': 57, 'script': 48, 'ul': 39, 'option': 27, 'tr': 22, 'table': 17, 'meta': 13, 'map': 12, 'area': 12, 'link': 11, 'style': 10, 'p': 10, 'b': 9, 'h2': 7, 'strong': 5, 'input': 2, 'body': 1, 'title': 1, 'html': 1, 'header': 1, 'form': 1, 'head': 1, 'label': 1, 'select': 1})

关于python - 在python中使用beautifulsoup提取网页的数据丰富节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29777794/

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