首页 > 编程语言 >Python 从在线网站获取当前日期时间

Python 从在线网站获取当前日期时间

时间:2023-10-31 10:33:34浏览次数:30  
标签:__ 在线 get Python ntp datetime 日期 time import

python3.7 获取网络时间_python 获取网络时间-CSDN博客

# -*- coding: utf-8 -*-
import datetime
import time

import ntplib
import requests


def get_beijing_time():
    try:
        url = 'https://beijing-time.org/'
        request_result = requests.get(url=url)
        if request_result.status_code == 200:
            headers = request_result.headers
            net_date = headers.get("date")
            gmt_time = time.strptime(net_date[5:25], "%d %b %Y %H:%M:%S")
            bj_timestamp = int(time.mktime(gmt_time) + 8 * 60 * 60)
            return datetime.datetime.fromtimestamp(bj_timestamp)
    except Exception as exc:
        return datetime.datetime.now()


def get_ntp_time():
    ntp_client = ntplib.NTPClient()
    response = ntp_client.request('pool.ntp.org')
    return datetime.datetime.fromtimestamp(response.tx_time)


if __name__ == '__main__':
    bj_time = get_beijing_time()
    print("北京时间: ", bj_time)
    ntp_time = get_ntp_time()
    print("ntp时间: ", ntp_time)

 

标签:__,在线,get,Python,ntp,datetime,日期,time,import
From: https://www.cnblogs.com/daizichuan/p/17799689.html

相关文章

  • python url 网址链接写函数()括号里不能访问显示403
    说明一则奇怪问题。同一个url网址链接,写到函数里就访问失败,写到变量里就可以正常访问。一、文件名test_url.pydefr_http(url): response=requests.get(url,headers=u_headers) print(f"response.status_code={response.status_code}")二、url直接写到调用的函数中,......
  • python编码规范
    遵循良好的编码风格,可以有效的提高代码的可读性,降低出错几率和维护难度。在团队开发中,使用(尽量)统一的编码风格,还可以降低沟通成本。网上有很多版本的编码规范,基本上都是遵循PEP8的规范:PEP0008–StyleGuideforPythonCodeGoogle的Python风格指南PythonGuide......
  • python求pi
    用python计算圆周率PI‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‮‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬描述用python......
  • 代码随想录训练营第二十天打卡(Python)| 654.最大二叉树 、617.合并二叉树 、700.二叉搜
    654.最大二叉树1、使用切片classSolution:defconstructMaximumBinaryTree(self,nums:List[int])->Optional[TreeNode]:iflen(nums)==0:returnNonemax_val=max(nums)max_index=nums.index(max_val)node=T......
  • Python 批量合并图片到word文档
    这段代码是一个用Python编写的功能,它将指定文件夹中的所有图片插入到Word文档中并保存。以下是代码的主要步骤和功能:导入必要的库Python中的docx库用于操作Word文档,glob库用于匹配文件路径。fromdocximportDocumentfromdocx.sharedimportInchesimportglob定义函数......
  • python sqlalchemy批量插入大量数据,性能最佳!
    defcreate_user_items(db:Session,mouse_events,user,events_dict):mouse_events=json.loads(mouse_events)db.execute(models.Sessions.__table__.insert(),[{"user_id":user,"x_coordinate":event["x_cor&q......
  • python sqlalchemy 动态设置表名__tablename__,一个model对应多个table
    fromsqlalchemyimportcreate_engine,Column,BigInteger,Stringfromsqlalchemy.ext.declarativeimportdeclarative_basefromsqlalchemy.ormimportsessionmakerbase=declarative_base()engine=create_engine("postgresql://postgresadmin:[email protected]......
  • Python使用selenium来自动化提交表单
    提问:怎么用selenium来自动化提交表单回答:fromseleniumimportwebdriverfromselenium.webdriver.common.keysimportKeys#创建一个Chrome浏览器实例driver=webdriver.Chrome(executable_path='path/to/chromedriver')#打开目标网页driver.get('https://www.example.c......
  • # yyds干货盘点 #Python自动化办公——3个Excel表格中每个门店物品不同,想要汇总在一起
    大家好,我是皮皮。一、前言前几天在Python白银交流群【上海新年人】问了一个Python自动化办公处理的问题,一起来看看吧。原始数据如下所示:二、实现过程这里【猫药师Kelly】给了一个代码和思路,如下所示:顺利地解决了粉丝的问题。下一篇文章,我们一起来看看另外一种方法。三、总结大家好,......
  • python最新采集京东app商品详情数据(2023-10-30)
    一、技术要点: 1、cookie可以从手机app端用charles抓包获取; 2、无需安装nodejs,纯python源码; 3、商品详情接口为:functionId="wareBusiness"; 4、clientVersion="10.1.4"同时也支持更高的版本; 5、sign签名算法已转成python源码;#-*-coding:UTF-8-*-importreques......