使用newspaper3k从新闻源获取更多文章URL?

2024-04-24 22:13:37 发布

您现在位置:Python中文网/ 问答频道 /正文

当我这样做的时候

import newspaper
paper = newspaper.build('http://cnn.com', memoize_articles=False)
print(len(paper.articles))

我看到那家报纸从http://cnn.com找到了902篇文章,考虑到他们每天发表很多文章,并且已经在网上发表了很多年,这对我来说似乎太少了。这些真的都是关于http://cnn.com的文章吗?如果没有,我有没有办法也找到其余文章的URL


Tags: importbuildcomfalsehttplen文章articles
1条回答
网友
1楼 · 发布于 2024-04-24 22:13:37

报纸仅查询CNN主页上的项目,因此该模块不会查询域上的所有类别(如业务、健康等)。根据我的代码,到今天为止,报纸只发现了698篇独特的文章。这些文章中的一些可能是相同的,因为有些URL有哈希,但看起来是同一篇文章

另外,您可以查询所有类别,但这需要报纸相结合

from newspaper import build

articles = []
urls_set = set()
cnn_articles = build('http://cnn.com', memoize_articles=False)
for article in cnn_articles.articles:
   # check to see if the article url is not within the urls_set
   if article.url not in urls_set:
     # add the unique article url to the set
     urls_set.add(article.url)
     articles.append(article.url)


print(len(articles))
# 698 

相关问题 更多 >