如何使用mwclient查找维基百科页面上的修订版本之间的文本差异?

问题描述 投票:1回答:1

我试图使用mwclient找到给定维基百科页面的两个修订版本之间的文本差异。我有以下代码:

import mwclient
import difflib

site = mwclient.Site('en.wikipedia.org')
page = site.pages['Bowdoin College']
texts = [rev for rev in page.revisions(prop='content')]
if not (texts[-1][u'*'] == texts[0][u'*']):
      ##show me the differences between the pages

谢谢!

python wikipedia-api
1个回答
1
投票

目前尚不清楚你需要使用difflib制作mwclient生成的差异或mediawiki生成的差异。

在第一种情况下,您有两个字符串(两个修订版本的文本),并且您希望使用difflib获取差异:

...
t1 = texts[-1][u'*']
t2 = texts[0][u'*']
print('\n'.join(difflib.unified_diff(t1.splitlines(), t2.splitlines())))

(difflib也可以生成HTML diff,请参阅文档以获取更多信息。)

但是如果你想使用mwclient生成MediaWiki生成的HTML diff,你需要修改ids

# TODO: Loading all revisions is slow,
# try to load only as many as required.
revisions = list(page.revisions(prop='ids'))  
last_revision_id = revisions[-1]['revid']
first_revision_id = revisions[0]['revid']

然后使用compare action来比较修订ID:

compare_result = site.get('compare', fromrev=last_revision_id, torev=first_revision_id)
html_diff = compare_result['compare']['*']
© www.soinside.com 2019 - 2024. All rights reserved.