在python中使用beautifulsoup提取网页的数据丰富节点,有没有办法统计页面中标签的频率,
import requests
from bs4 import BeautifulSoup
url = "http://www.amazon.in"
r = requests.get(url)
html = BeautifulSoup(r.content)
现在我想统计获得的html中所有标签的频率。
最佳答案
使用dict comprehensions和 collection.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/