报纸上的Python缓存问题,每一个调用相同的输出

2024-04-16 04:37:49 发布

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

我使用这个模块:https://github.com/codelucas/newspaperhttps://news.bitcoin.com/下载比特币文章。但当我试图从下一页https://news.bitcoin.com/page/2/page获取下一篇文章时,我得到了相同的结果。其他页面也一样。你知道吗

我尝试了不同的网站和不同的起始页。我使用的第一个链接的文章显示在所有其他链接上。你知道吗

import newspaper

url = 'https://news.bitcoin.com/page/2'
btc_articles = newspaper.build(url, memoize_articles = False)

for article in btc_articles.articles:
    print(article.url)

Tags: 模块httpsgithubcomurl链接article文章
1条回答
网友
1楼 · 发布于 2024-04-16 04:37:49

报纸图书馆试图搜集整个网站,而不仅仅是你输入的链接。这意味着您不必遍历所有页面来获取文章。 但是,正如您可能已经注意到的,lib并没有找到所有的文章。你知道吗

这样做的原因似乎是它没有将所有页面标识为类别(也没有找到提要),请参见下文(无论页面如何,输出都是相同的):

import newspaper

url = 'https://news.bitcoin.com/'
btc_paper = newspaper.build(url, memoize_articles = False)

print('Categories:', [category.url for category in btc_paper.categories])
print('Feeds:', [feed.url for feed in btc_paper.feeds])

输出:

Categories: ['https://news.bitcoin.com/page/2', 'https://news.bitcoin.com']
Feeds: []

正如您在故障报告https://github.com/codelucas/newspaper/issues/670中所指出的那样,这似乎是代码中的一个bug(或者是比特币上糟糕的网站设计,具体取决于您如何看待它)。你知道吗

相关问题 更多 >