如何访问 newspaper3k 中的缓存文章

How to access cached articles in newspaper3k

Newspaper 是一个很棒的库,它允许抓取网络数据,但是我对文章缓存有点困惑。它缓存文章以加快操作,但我如何访问这些文章?

我有这样的东西。现在,当我对同一组文章执行两次 运行 此命令时,我第二次得到 return 类型 None。如何访问那些以前缓存的文章进行处理?

newspaper_articles = [Article(url) for url in links]

看看这个:https://github.com/codelucas/newspaper/issues/481 it seems the caching method 'cache_disk' in https://github.com/codelucas/newspaper/blob/master/newspaper/utils.py 可能有错误。它确实会将结果缓存到磁盘(搜索文件夹“.newspaper_scraper”),但之后不会加载它们。

解决方法是在构建报纸时设置 memoize_articles=False,或者使用配置 class。

newspaper.build(url, memoize_articles=False)

查看源代码后,视情况而定。

https://github.com/codelucas/newspaper/blob/beacce0e167349374ce0b37012b01c7c07a26890/newspaper/settings.py#L35

DATA_DIRECTORY = '.newspaper_scraper'

TOP_DIRECTORY = os.path.join(tempfile.gettempdir(), DATA_DIRECTORY)

因此 运行 在您的 python 解释器中获取缓存位置

import tempfile
tempfile.gettempdir()