当我这么做
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中,但我想得到任何新闻来源的文章时间戳。如果可能的话,我想知道日期和时间。
发布于 2020-09-22 03:08:58
您可以使用下面的代码使用报纸检索文章的发布日期。我重新格式化了日期输出,因为它们有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中获得这些日期。在查看了报纸的代码之后,我发现了一个元标签提取器。
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')https://stackoverflow.com/questions/63948084
复制相似问题