API请求参数被忽略

发布于 2025-02-04 03:32:18 字数 658 浏览 2 评论 0原文

该代码按预期工作,并显示了3个最近的Wikipedia编辑。

我的问题是,如果我删除第二个URL线,则如果未列出用户,我应该获得三次或无的 urmi27 。 但是我得到两个URL的列表。 API请求忽略了“动作”?

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'])

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'])

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情未る 2025-02-11 03:32:18

个位置(在URL和params dictionary中定义获取参数

您是在2 代码> feedContributions 操作使用不同的参数和不同的返回格式大不相同。

要使用feedContributions操作,您需要这样的东西:

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)
   

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文