How to get the current time in Python

前端 未结 30 1956
滥情空心
滥情空心 2020-11-22 06:47

What is the module/method used to get the current time?

30条回答
  •  南方客
    南方客 (楼主)
    2020-11-22 07:31

    Why not ask the U.S. Naval Observatory, the official timekeeper of the United States Navy?

    import requests
    from lxml import html
    
    page = requests.get('http://tycho.usno.navy.mil/cgi-bin/timer.pl')
    tree = html.fromstring(page.content)
    print(tree.xpath('//html//body//h3//pre/text()')[1])
    

    If you live in the D.C. area (like me) the latency might not be too bad...

提交回复
热议问题