微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

从Python中的Wikipedia API获取时间戳

如何解决从Python中的Wikipedia API获取时间戳

我正在尝试从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)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。