从Python中的WikipediaAPI获取时间戳

从Python中的WikipediaAPI获取时间戳,python,json,wikipedia-api,Python,Json,Wikipedia Api,我试图从WikipediaAPI中获取时间戳,并将其拆分为格式(y-m-d),但仍然找不到解决方法 import requests S = requests.Session() URL = "https://en.wikipedia.org/w/api.php?" PARAMS = { 'action':'query', 'prop':'revisions', 'rvlimit':'1', 'rvprop':"timestamp|u

我试图从WikipediaAPI中获取时间戳,并将其拆分为格式(y-m-d),但仍然找不到解决方法

import requests

S = requests.Session()
URL = "https://en.wikipedia.org/w/api.php?"
PARAMS = {
    'action':'query',
    'prop':'revisions',
    'rvlimit':'1',
    'rvprop':"timestamp|user|comment|content",
    'rvdir': 'newer',
    'format':'json',
    'titles': 'Brno'
}
R = S.get(url=URL, params=PARAMS)
DATA = R.json()

PAGES = DATA["query"]["pages"]
print(PAGES)
for page in PAGES:
    print(page)
以下是输出:

{'57575': {'pageid': 57575, 'ns': 0, 'title': 'Brno', 'revisions': [{'user': 'Jeronimo', 'timestamp': '2002-06-16T13:40:19Z', 'contentformat': 'text/x-wiki', 'contentmodel': 'wikitext', 'comment': '*', '*': "'''Brno''' (population 390,000, [[German language|German]]: ''Brünn'') is the second largest city of the [[Czech Republic]], located in the southeast of the country, at the confluence of the [[Svitava]] and [[Svratka]] rivers.\r\n"}]}}
57575

假设循环将在代码运行时正确循环多个页面,则可以将for循环更改为:

for page in PAGES:
    date = PAGES[page]['revisions'][0]['timestamp']

    # Example date = 2002-06-16T13:40:19Z' Split on T to get the YYYY-MM-DD first
    formatted_date  = date.split("T")[0] 

    print(formatted_date)

谢谢@Nebulous29的回答,但是有一个类似这样的错误:
TypeError:“type”对象不可下标
,似乎无法到达时间戳。是的,这是一个复制粘贴错误。我在测试时更改了一些变量名,我肯定错过了一个。我已经编辑了答案-让我知道你的进展如何!