Abh*_*tia 13 python urllib2 bs4
我正在从html文件中读取文本并进行一些分析.这些.html文件是新闻文章.
码:
html = open(filepath,'r').read()
raw = nltk.clean_html(html)
raw.unidecode(item.decode('utf8'))
Run Code Online (Sandbox Code Playgroud)
现在我只想要文章内容,而不是广告,标题等其他文本.我怎么能在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-高癌症risk.html?SRC =我和REF =一般
Har*_*rry 12
报纸越来越受欢迎,我只是表面上看,但它看起来不错.它只是Python 3.
快速入门仅显示从URL加载,但您可以从HTML字符串加载:
import newspaper
# LOAD HTML INTO STRING FROM FILE...
article = newspaper.Article('') # STRING REQUIRED AS `url` ARGUMENT BUT NOT USED
article.set_html(html)
Run Code Online (Sandbox Code Playgroud)
在Python中也有这个库:)
既然你提到了Java,就有一个用于samppipe的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
Run Code Online (Sandbox Code Playgroud)