首页 > 解决方案 > 使用报纸3k 从新闻来源获取更多文章 URL?

问题描述

当我做

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

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

标签: pythonpython-newspapernewspaper3k

解决方案


报纸只是查询CNN主页上的项目,所以模块不会查询域上的所有类别(例如商业、健康等)。根据我的代码,截至今天,只有 698 篇独特的文章被Newspaper发现。其中一些文章可能是相同的,因为一些 URL 具有哈希值,但看起来是同一篇文章。

PS您可以查询所有类别,但这需要Selenium加上Newspaper

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 

推荐阅读