Skip to main content
 首页 » 编程设计

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

2024年10月25日17think

在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})