4

我正在尝试将 Python 图书馆报纸与来自Wayback Machine的档案一起使用,该档案存储了已归档网站的旧版本。理论上,可以从这些档案库中查询和下载旧的新闻文章。

例如,以下代码查询CNBC的档案以获取特定的存档日期。

import newspaper
url = 'http://web.archive.org/web/20161201123529/http://www.cnbc.com/'
paper = newspaper.build(url, memoize_articles = False )

尽管存档的网站本身包含 2016-12-01 的实际新闻文章的链接,但报纸模块似乎没有接收到它们。相反,您会获得以下网址:

https://blog.archive.org/2016/10/23/defining-web-pages-web-sites-and-web-captures/

这些不是来自此 CNBC 存档版本的实际文章。但是,报纸与今天CNBC版本配合得很好。

我想它会因为 url 的格式(包含两个https)而感到困惑。有人对如何从Wayback Machine档案中提取文章有任何建议吗?

4

1 回答 1

1

这是一个有趣的问题,我将把它添加到GitHub 上的报纸使用概述文档中。

我试图使用报纸.build,但我无法让它正常工作,所以我使用了报纸来源。

from time import sleep
from random import randint
from newspaper import Config
from newspaper import Source

USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:78.0) Gecko/20100101 Firefox/78.0'

config = Config()
config.browser_user_agent = USER_AGENT
config.request_timeout = 10

wayback_cnbc = Source(url='https://web.archive.org/web/20180301012621/https://www.cnbc.com/', config=config,
                  memoize_articles=False, language='en', number_threads=20, thread_timeout_seconds=2)

wayback_cnbc.build()
for article_extract in wayback_cnbc.articles:
   article_extract.download()
   article_extract.parse()

   print(article_extract.publish_date)
   print(article_extract.title)
   print(article_extract.url)
   print('')

   # this sleep timer is helping with some timeout issues
   # that were happening when querying
   sleep(randint(1,3))

上面的示例输出如下:

None
Media
https://web.archive.org/web/20180301012621/https://www.cnbc.com/media/
    
None
CNBC Video
https://web.archive.org/web/20180301012621/https://www.cnbc.com/video/

2017-11-08 00:00:00
CNBC Healthy Returns
https://web.archive.org/web/20180301012621/https://www.cnbc.com/2017/11/08/healthy-returns.html

2018-02-28 00:00:00
Markets in Asia decline as dollar steadies; Nikkei falls 307 points 
https://web.archive.org/web/20180301012621/https://www.cnbc.com/2018/02/28/asia-markets-stocks-dollar-and-china-caixin-pmi-in-focus.html

2018-02-28 00:00:00
S&P 500 rises, but on track to snap longest monthly win streak since 1959
https://web.archive.org/web/20180301012621/https://www.cnbc.com/2018/02/28/us-stocks-interest-rates-fed-markets.html
     

希望这个答案有助于您在 WayBack Machine 中查询文章的用例。如果您有任何问题,请告诉我。

于 2020-12-28T11:22:41.873 回答