开发者

Wikipedia API: how to get the number of revisions of a page?

开发者 https://www.devze.com 2023-03-29 08:10 出处:网络
Anyone know how to get the number of re开发者_运维知识库visions of a wikipedia page using mediawiki API?

Anyone know how to get the number of re开发者_运维知识库visions of a wikipedia page using mediawiki API? I have read this API documentation, but can't find the related API:

Revision API


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.


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)


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>


With REST API provided by newer MediaWikis, you can use the "Get page history counts" API to get the number of revisions of a page.

For example,

GET https://en.wikipedia.org/w/rest.php/v1/page/Jupiter/history/counts/edits?from=384955912&to=406217369

this request will return a JSON response like the following:

{
    "count": 110,
    "limit": false
}

Zero coding at all.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号