python - 使用报纸3k 从新闻来源获取更多文章 URL?
问题描述
当我做
import newspaper
paper = newspaper.build('http://cnn.com', memoize_articles=False)
print(len(paper.articles))
我看到那家报纸从http://cnn.com找到了902篇文章,这对我来说似乎很少,考虑到他们每天发表很多文章,并且已经在网上发表了很多年的文章。这些真的是http://cnn.com上的所有文章吗?如果没有,有什么办法我也可以找到其余文章的网址吗?
解决方案
报纸只是查询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
推荐阅读
- python-3.x - 在读取多个 csv 文件时获取错误标记数据
- c - 如何使我的代码符合 MISRA 2012 RULE 8.4
- azure - 无法访问 Azure Databricks 根/本地存储
- wmi - 如何在 wmi_exporter 查询中过滤正在运行的进程?
- javascript - 将 If 语句写入 = 一个数字但不允许比另一个
- yarnpkg - Yarn,“未满足的对等依赖项”是什么意思
- android - 如何将函数从子类传递给父类,我收到此错误:在构建期间调用 setState() 或 markNeedsBuild()
- python-asyncio - 从在单独线程中运行的同步代码中将项目放入 asyncio.Queue 的最简单方法
- c# - 如何在另一个 Vue 中访问一个 Vue?
- flutter - 响应 TextFormField 中的“完成”按钮