下载维基百科页面的整个历史

Download entire history of a Wikipedia page

我想下载维基百科上单篇文章的整个修订历史,但 运行 遇到了障碍。

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

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

当然,我可以下载 整个 站点,包括 here 中每篇文章的所有版本,但这比我需要的数据要多很多 TB。

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

漫无目的地四处游荡,寻找我自己的另一个问题的线索——我的方式是说我对这个话题一无所知! — 我刚刚在阅读您的问题后突然想到这个: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('WhosebugRevisions.pkl', 'wb'))

上面的示例仅获取有关修订的信息,而不是实际内容本身。这是一个简短的 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))