信息提取的一般方法

xiaoxiao2021-02-28  29

方法一:完整解析信息的标记形式,再提取关键信息。

XML    JSON    YAML

需要标记解析器 

eg: bs4库的标签树遍历

优点: 信息解析准确

缺点: 提取过程繁琐,速度慢。

方法二: 无视任何标记形式,直接搜索关键信息。

搜索

对信息的文本查找函数即可。

优点: 提取过程简洁,速度较快。

缺点:提取结果准确性与信息内容相关。

融合方法

融合方法: 结合形式解析与搜索方法,提取关键信息。

XML    JSON    YAML     搜索

需要标记解析器及文本查找函数。

实例

提取HTML中所有URL链接

思路: 1)搜索到所有<a>标签

           2)解析<a>标签格式,提取href后的链接内容。

>>> from bs4 import BeautifulSoup >>> import requests >>> r = requests.get("http://python123.io/ws/demo.html") >>> demo = r.text >>> soup = BeautifulSoup(demo,"html.parser") >>> for link in soup.find_all('a'): print(link.get('href')) #在a标签中获取它的属性 http://www.icourse163.org/course/BIT-268001 http://www.icourse163.org/course/BIT-1001870001 >>>
转载请注明原文地址: https://www.6miu.com/read-2799967.html

最新回复(0)