-->

Wikipedia API: how to get the number of revisions

2019-04-06 20:50发布

问题:

Anyone know how to get the number of revisions of a wikipedia page using mediawiki API? I have read this API documentation, but can't find the related API:
Revision API

回答1:

The only possibility is to retrieve all revisions and count them. You might need to continue the query for that.

Bug 17993 is about including a count, but is still unsolved.



回答2:

Here is code to get number of revisions of a page (in this case, the JSON wiki page):

import requests

BASE_URL = "http://en.wikipedia.org/w/api.php"
TITLE = 'JSON'

parameters = { 'action': 'query',
           'format': 'json',
           'continue': '',
           'titles': TITLE,
           'prop': 'revisions',
           'rvprop': 'ids|userid',
           'rvlimit': 'max'}

wp_call = requests.get(BASE_URL, params=parameters)
response = wp_call.json()

total_revisions = 0

while True:
  wp_call = requests.get(BASE_URL, params=parameters)
  response = wp_call.json()

  for page_id in response['query']['pages']:
    total_revisions += len(response['query']['pages'][page_id]['revisions'])

  if 'continue' in response:
    parameters['continue'] = response['continue']['continue']
    parameters['rvcontinue'] = response['continue']['rvcontinue']

  else:
    break

print parameters['titles'], total_revisions

You can check the result here: https://en.wikipedia.org/w/index.php?title=JSON&action=info#Edit_history

(accessible from the corresponding wikipedia page sidebar: Tools - Page information)



回答3:

Retrieve the revisions and implement a method to count them (It's just XML).

MediaWiki Revisions: Example

api.php ? action=query & prop=revisions & titles=API|Main%20Page & rvprop=timestamp|user|comment|content

.

<api>
<query>
<pages>
  <page pageid="1191" ns="0" title="API">
    <revisions>
      <rev user="Harryboyles" timestamp="2006-10-31T05:39:01Z" comment="revert unexplained change: see talk ...">
        ...content...
      </rev>
    </revisions>
  </page>
  <page pageid="11105676" ns="0" title="Main Page">
    <revisions>
      <rev user="Ryan Postlethwaite" timestamp="2007-06-26T19:05:06Z" comment="rv - what was that for?">
        ...content...
      </rev>
    </revisions>
  </page>
</pages>