【问题标题】:python requests cookies date formatpython请求cookies日期格式
【发布时间】:2015-05-27 18:48:29
【问题描述】:

我在请求的帮助下从站点获取 cookie。当我打印 cookie 密钥时,我得到以下信息:

[Cookie(version=0, name='Session', value='4safsadfsdfsdfsd06206ba8cea7c2', port=None, port_specified=False, domain='.site.info', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1433352401, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False),

 Cookie(version=0, name='UserID', value='555777', port=None, port_specified=False, domain='.site.info', domain_specified=True, domain_initial_dot=True, path='/', path_specified=True, secure=False, expires=1433352401, discard=False, comment=None, comment_url=None, rest={}, rfc2109=False) ]

这是什么意思? 过期=1433352401 这是什么日期格式?如何将其转换为通常的正常日期?

【问题讨论】:

    标签: python date python-3.x cookies python-requests


    【解决方案1】:

    格式是 Unix 纪元格式,称为 Unix 时间。

    您可以通过以下方式在 Python 2 中转换纪元时间:

    import datetime
    print(datetime.datetime.fromtimestamp(int(cookie_time)).strftime('%Y-%m-%d %H:%M:%S'))
    

    Cookie 在 2015-06-03 17:26:41 到期,即 6 月 3 日。

    您可以在此处实时运行此代码:http://ideone.com/a4eC0Y

    【讨论】:

      【解决方案2】:

      这是一个时间戳;自纪元以来的秒数。您可以使用datetime.fromtimestamp() 方法从中创建一个datetime 对象:

      >>> from datetime import datetime
      >>> datetime.fromtimestamp(1433352401)
      datetime.datetime(2015, 6, 3, 18, 26, 41)
      

      cookie 将于今年 6 月 3 日到期。

      【讨论】:

        猜你喜欢
        • 2014-09-25
        • 1970-01-01
        • 1970-01-01
        • 2012-10-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-18
        • 2012-08-10
        相关资源
        最近更新 更多