Python使用newspaper3k对文章进行标题图片关键词文本等提取

Python使用newspaper3k对文章进行标题图片关键词文本等提取

1. 效果图

在这里插入图片描述

首图如下:
在这里插入图片描述
在这里插入图片描述

2. 安装及报错解决

python 3.7.4

pip install newspaper3k==0.2.8

3. 源码

import newspaper
from newspaper import Config

sina_paper = newspaper.build('http://www.sina.com.cn/', language='zh')

for category in sina_paper.category_urls():
    print(category)
# http://health.sina.com.cn
# http://eladies.sina.com.cn
# http://english.sina.com

user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15'
user_agent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/98.0.4758.102 Safari/537.36'

config = Config()
config.browser_user_agent = user_agent

article = sina_paper.articles[0]
article.config = config
print(len(article.url), article.url)
if (article.url.find("http") > 4): article.url = 'http' + article.url.split("http")[2]

print(len(article.url), article.url)
article.url = article.url.replace(' ', '')
print(len(article.url), article.url)
article.download()
article.parse()

print('text: ', article.text)
print('title: ', article.title)
print('publish_date: ', article.publish_date)
print('tags: ', article.tags)
print('top_image: ', article.top_image)
print('top_img: ', article.top_img)
print('url: ', article.url)
print('meta_keywords: ', article.meta_keywords)
print('meta_img: ', article.meta_img)
print('keywords: ', article.keywords)
print('images:', len(article.images), article.images)
print('imgs', len(article.imgs), article.imgs)


# url = "https://www.newsweek.com/new-mexico-compound-charges-dropped-children-1096830".strip()
#
# content = []
# for i in [article.url]:
#     article = Article(str(i), config=config)
#     try:
#         article.download()
#         article.parse()
#         article.download()
#         print(article.html)
#         article.parse()
#         text = article.text
#         print(i, text)
#         content.append(text)
#     except Exception as e:
#         print(e)
#         content.append('error')
#     continue
# print(content)

参考

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

程序媛一枚~

您的鼓励是我创作的最大动力。

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值