Home > Back-end >  api request parameters are ignored
api request parameters are ignored

Time:06-02

This code works as expected and shows 3 recent wikipedia editors.

My question is that if I uncomment the second URL line, I should get Urmi27 three times or None if the user is not listed. But I get the same list for both URL. Is "action" ignored by api request?

import requests

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"

#URL = "https://en.wikipedia.org/w/api.php?action=feedcontributions&user=Urmi27"

PARAMS = {
    "format": "json",
    "rcprop": "comment|loginfo|title|ids|sizes|flags|user",
    "list": "recentchanges",
    "action": "query",
    "rclimit": "3"
}

R = S.get(url=URL, params=PARAMS)
DATA = R.json()

RECENTCHANGES = DATA['query']['recentchanges']

for rc in RECENTCHANGES:
    print (rc['user'])

CodePudding user response:

You are defining GET parameter in 2 places (in the URL and in the PARAMS dictionary) and the API is prioritizing the PARAMS

The query and feedcontributions actions are very different, using different parameters and different return formats.

To use the feedcontributions action, you would need something like this:

import requests
import xml.etree.ElementTree as ET

S = requests.Session()

URL = "https://en.wikipedia.org/w/api.php"


PARAMS = {
"action":"feedcontributions",
"user":"Urmi27"
}

R = S.get(url=URL, params=PARAMS)
xml_tree = ET.fromstring(R.content)


for child in xml_tree:
print(child.tag, child.attrib)
for channel in child:
    for elements in channel:
        if elements.tag == "description":
            print(elements.text)
   

API REF

  • Related