从存储的.html页面中提取新闻文章内容
我正在从html文件中读取文本并进行一些分析。这些.html文件是新闻文章。
码:
html = open(filepath,'r').read()
raw = nltk.clean_html(html)
raw.unidecode(item.decode('utf8'))
现在,我只想要文章的内容,而不是广告,标题等文本的其余部分。我如何在python中相对准确地做到这一点?
我知道一些工具,例如Jsoup(java
API)和bolier,但我想在python中这样做。我可以找到一些使用bs4的技术,但仅限于一种类型的页面。我有来自众多来源的新闻页面。另外,也没有任何示例代码示例。
我正在寻找与python中完全类似的内容http://www.psl.cs.columbia.edu/wp-
content/uploads/2011/03/3463-WWWJ.pdf。
编辑:
为了更好地理解,请编写示例代码以提取以下链接的内容http://www.nytimes.com/2015/05/19/health/study-
finds-dense-breast-tissue-isnt-always -a-high-cancer-risk.html?src = me&ref =
general
-
Python中也有此库:)
自从您提到Java以来,有一个适用于样板程序的Python包装器,可让您在python脚本中直接使用它:https : //github.com/misja/python-
boilerpipe如果要使用纯python库,则有2个选项:
https://github.com/buriy/python-readability
和
https://github.com/grangier/python-goose
在这两者中,我更喜欢Goose,但是请注意,由于某些原因,它的最新版本有时无法提取文本(我的建议是现在使用1.0.22版)。
编辑:这是使用Goose的示例代码:
from goose import Goose from requests import get response = get('http://www.nytimes.com/2015/05/19/health/study-finds-dense-breast-tissue-isnt-always-a-high-cancer-risk.html?src=me&ref=general') extractor = Goose() article = extractor.extract(raw_html=response.content) text = article.cleaned_text