【问题标题】:Download entire history of a Wikipedia page下载维基百科页面的整个历史
【发布时间】:2017-07-19 14:04:43
【问题描述】:

我想下载 Wikipedia 上一篇文章的完整修订历史记录,但遇到了障碍。

下载整篇维基百科文章或使用Special:Export URL 参数获取其历史片段非常容易:

curl -d "" 'https://en.wikipedia.org/w/index.php?title=Special:Export&pages=Stack_Overflow&limit=1000&offset=1' -o "StackOverflow.xml"

当然,我可以下载整个网站,包括来自here 的每篇文章的所有版本,但这已经超过了我需要的数 TB 数据量。

是否有预先构建的方法来执行此操作? (好像应该有。)

【问题讨论】:

    标签: web-scraping wikipedia


    【解决方案1】:

    上面的示例仅获取有关修订的信息,而不是实际内容本身。这是一个简短的 Python 脚本,可将页面的完整内容和元数据历史数据下载到单独的 json 文件中:

    import mwclient
    import json
    import time
    
    site = mwclient.Site('en.wikipedia.org')
    page = site.pages['Wikipedia']
    
    for i, (info, content) in enumerate(zip(page.revisions(), page.revisions(prop='content'))):
        info['timestamp'] = time.strftime("%Y-%m-%dT%H:%M:%S", info['timestamp'])
        print(i, info['timestamp'])
        open("%s.json" % info['timestamp'], "w").write(json.dumps(
            { 'info': info,
                'content': content}, indent=4))
    

    【讨论】:

      【解决方案2】:

      漫无目的地四处寻找我自己的另一个问题的线索——我的说法是我对这个话题一无所知! — 在阅读了您的问题后,我才想到这一点:http://mwclient.readthedocs.io/en/latest/reference/page.html。看看revisions 方法。

      编辑:我也看到http://mwclient.readthedocs.io/en/latest/user/page-ops.html#listing-page-revisions

      使用mwclient 模块的示例代码:

      import mwclient, pickle
      
      print 'getting page...'
      site = mwclient.Site(('https', 'en.wikipedia.org'))
      page = site.pages['Stack_Overflow']
      
      print 'extracting revisions (may take a really long time, depending on the page)...'
      revisions = []
      for i, revision in enumerate(page.revisions()):
          revisions.append(revision)
      
      print 'saving to file...'
      pickle.dump(revisions, open('StackOverflowRevisions.pkl', 'wb'))
      

      【讨论】:

      • 这太好了,谢谢比尔!为了完整起见,在您的答案中添加一些示例代码。
      • 不客气,干得好!我正要自己添加一些。
      猜你喜欢
      • 1970-01-01
      • 2010-12-26
      • 1970-01-01
      • 1970-01-01
      • 2011-09-21
      • 1970-01-01
      • 1970-01-01
      • 2015-05-08
      • 2016-03-27
      相关资源
      最近更新 更多