Python: 看到 newspaper3k 提供的文章时间戳了吗?

Python: See timestamp of article provided by newspaper3k?

当我做的时候

import newspaper
cnn_paper = newspaper.build(news_source_url, memoize_articles=False)
for article in cnn_paper.articles:
    print(article.url)
exit()

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

特别是对于 CNN,日期似乎编码在许多文章的 URL 中,但我想获取 any 新闻源的文章时间戳。如果可能的话,我想同时获取日期和时间。

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

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 获取这些信息。在查看 Newspaper 的代码后,我发现了一个元标记提取器。

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')