前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python3 使用newspaper库提取新闻内容(readability,jparser)

python3 使用newspaper库提取新闻内容(readability,jparser)

作者头像
周小董
发布2019-08-29 14:01:28
2.6K0
发布2019-08-29 14:01:28
举报
文章被收录于专栏:python前行者python前行者
  • GitHub:https://github.com/codelucas/newspaper
  • Newspaper文档说明:https://newspaper.readthedocs.io/en/latest/
  • Newspaper快速入门:https://newspaper.readthedocs.io/en/latest/user_guide/quickstart.html

Newspaper是一个python3库。

注:Newspaper框架并不适用于实际工程类新闻信息爬取工作,框架不稳定,爬取过程中会有各种bug,例如获取不到url、新闻信息等,但对于想获取一些新闻语料的朋友不妨一试,简单方便易上手,且不需要掌握太多关于爬虫方面的专业知识。

安装

代码语言:javascript
复制
pip3 install newspaper3k
or 
pip3 install --ignore-installed --upgrade newspaper3k

如果文章没有指明使用的什么语言的时候,Newspaper会尝试自动识别。支持10多种语言并且所有的都是unicode编码。

代码语言:javascript
复制
import time
from newspaper import Article

url = 'https://www.chinaventure.com.cn/news/78-20190819-347269.html'
url='https://36kr.com/p/5237348'
# 创建文章对象
news = Article(url, language='zh')
# 下载网页
news.download()
## 网页解析
news.parse()
print("title=",news.title)# 获取文章标题
print("author=", news.authors)   # 获取文章作者
print("publish_date=", news.publish_date)   # 获取文章日期
# 自然语言处理
news.nlp()
print('keywords=',news.keywords)#从文本中提取关键字
print("summary=",news.summary)# 获取文章摘要
# time.sleep(30)
print("text=",news.text,"\n")# 获取文章正文

print("movies=",news.movies)   # 获取文章视频链接
print("top_iamge=",news.top_image)   # 获取文章顶部图片地址
print("images=",news.images)#从html中提取所有图像
print("imgs=",news.imgs)
print("html=",news.html)#获取html

也可以直接导入包,如果语言是一致的,也可以直接声明

代码语言:javascript
复制
import newspaper

url='http://www.coscocs.com/'
'''注:文章缓存:默认情况下,newspaper缓存所有以前提取的文章,并删除它已经提取的任何文章。
此功能用于防止重复的文章和提高提取速度。可以使用memoize_articles参数选择退出此功能。'''
news = newspaper.build(url, language='zh', memoize_articles=False)
article = news.articles[0]
article.download()
article.parse()
print('text=',article.text)
print('brand=',news.brand)  #提取源品牌
print('description=',news.description) # 提取描述
print("一共获取%s篇文章" % news.size())  # 文章的数目

# 所有文章的url
for article in news.articles:
    print(article.url)

#提取源类别
for category in news.category_urls():
    print(category)

#提取源提要
for feed_url in news.feed_urls():
    print(feed_url)

注:文章缓存:默认情况下,newspaper缓存所有以前提取的文章,并删除它已经提取的任何文章。此功能用于防止重复的文章和提高提取速度。可以使用memoize_articles参数选择退出此功能。

Requests和Newspaper结合解析正文

代码语言:javascript
复制
import requests
from newspaper import fulltext

html = requests.get('https://www.washingtonpost.com/business/economy/2019/01/17/19662748-1a84-11e9-9ebf-c5fed1b7a081_story.html?utm_term=.26198c91916f').text
text = fulltext(html)
print(text)

Google Trends信息

代码语言:javascript
复制
import newspaper

print(newspaper.languages())#获取支持的语言
print(newspaper.hot())#hot()使用公共api返回谷歌上的热门词汇列表
print(newspaper.popular_urls())#popular_urls()返回一个流行新闻源url列表

多任务

代码语言:javascript
复制
import newspaper
from newspaper import news_pool

# 创建并行任务
slate_paper = newspaper.build('http://slate.com')
tc_paper = newspaper.build('http://techcrunch.com')
espn_paper = newspaper.build('http://espn.com')

papers = [slate_paper, tc_paper, espn_paper]
news_pool.set(papers, threads_per_source=2) # (3*2) = 6 共6个线程

news_pool.join()

print(slate_paper.articles[10].html)

python-readability

github地址是:https://github.com/buriy/python-readability

安装

代码语言:javascript
复制
pip install requests
pip install readability-lxml

使用方式:

代码语言:javascript
复制
import requests
from readability import Document
response = requests.get('https://news.163.com/18/1123/13/E1A4T8F40001899O.html')
doc = Document(response.text)
print doc.title()
print doc.content()

测试结果:正文提取范围太大,且乱码,不好用,拿到的正文部分,明显有问题。而且还有乱码问题。所以,不建议使用。

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019年08月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Requests和Newspaper结合解析正文
  • Google Trends信息
  • 多任务
  • python-readability
相关产品与服务
NLP 服务
NLP 服务(Natural Language Process,NLP)深度整合了腾讯内部的 NLP 技术,提供多项智能文本处理和文本生成能力,包括词法分析、相似词召回、词相似度、句子相似度、文本润色、句子纠错、文本补全、句子生成等。满足各行业的文本智能需求。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档