问题描述
我正在尝试从Wikipedia-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)