代码之家  ›  专栏  ›  技术社区  ›  shantanuo

忽略api请求参数

  •  0
  • shantanuo  · 技术社区  · 4 年前

    这段代码按预期工作,并显示了3个最新的维基百科编辑器。

    我的问题是,如果我取消对第二个URL行的注释,我应该得到 Urmi27 三次或无 如果用户未列出。 但我得到的两个URL的列表相同。api请求是否忽略了“action”?

    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'])
    
    0 回复  |  直到 4 年前
        1
  •  1
  •   Clifford Cheefoon    4 年前

    您在两个位置定义GET参数(在URL和 PARAMS 字典),并且API正在对 PARAMS

    这个 query 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)
       

    API REF

    推荐文章