首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python:参见newspaper3k提供的文章的时间戳吗?

Python:参见newspaper3k提供的文章的时间戳吗?
EN

Stack Overflow用户
提问于 2020-09-18 01:20:17
回答 1查看 934关注 0票数 0

当我这么做

代码语言:javascript
复制
import newspaper
cnn_paper = newspaper.build(news_source_url, memoize_articles=False)
for article in cnn_paper.articles:
    print(article.url)
exit()

我可以从news_source_url (例如,'http://cnn.com')下载文章的URL列表,这些文章可以使用newspaper3k包下载。有什么办法可以得到各种物品的时间戳吗?

对于CNN具体而言,日期似乎被编码在许多文章的URL中,但我想得到任何新闻来源的文章时间戳。如果可能的话,我想知道日期和时间。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-09-22 03:08:58

您可以使用下面的代码使用报纸检索文章的发布日期。我重新格式化了日期输出,因为它们有00:00:00时间戳。

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

cnn_paper = newspaper.build('http://cnn.com', memoize_articles=False)
for item in cnn_paper.articles:
  article = newspaper.Article(item.url)
  article.download()
  article.parse()
  if article.url and article.publish_date is not None:
    print(article.url)
    publish_date = datetime.strptime(str(article.publish_date), '%Y-%m-%d %H:%M:%S').strftime('%Y-%m-%d')
    print(publish_date)

如果您需要文章的确切发布日期和时间戳,那么您需要从文章的URL中获得这些日期。在查看了报纸的代码之后,我发现了一个元标签提取器。

代码语言:javascript
复制
import newspaper

cnn_paper = newspaper.build('http://cnn.com', memoize_articles=False)
for item in cnn_paper.articles:
   article = newspaper.Article(item.url)
   article.download()
   article.parse()
   if article.url and article.publish_date is not None:
     article_meta_data = article.meta_data
     article_published_date = sorted({value for (key, value) in article_meta_data.items() if key == 'pubdate'})
     if article_published_date:
        print(article_published_date)
     else:
        print('no published date provided')
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63948084

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档