从 Python 中的维基百科 API 获取时间戳

Get timestamp from Wikipedia API in Python

我正在尝试从维基百科获取时间戳-api 并将其拆分为 (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)