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

Extract the data rich nodes of a webpage using beautifulsoup in python

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

import requests
from bs4 import BeautifulSoup

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

html = BeautifulSoup(r.content)

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

使用 dict comprehensions and collection.Counter 获取 tags 的计数,它们是 bs4.element.Tag.

的实例
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})