时间戳

import urllib.request
import re
headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0'}
r =  urllib.request.Request('https://m.weibo.cn/api/container/getIndex?containerid=102803_ctg1_4388_-_ctg1_4388&openApp=0',headers=headers)
page = urllib.request.urlopen(r)
html = page.read()
import json
m = json.loads(html)
mm = m['data']

for t in mm['cards']:
    tt = t['mblog']
    title  = re.sub("<[^>]*>", "", tt['text'])
    #print(tt['created_at'])
    #print(tt['source'])
   # print(tt['user'][ 'screen_name'])
    '''
    import time
    t = tt['created_at']
    min = re.findall('\d+', t)[0]
    if u'分钟' in t:
        c = time.time() - int(min) * 60
    elif u'小时' in t:
        c = time.time() - int(min) * 60 * 60
    elif u'天' in t:
        c = time.time() - int(min) * 60 * 60 * 24
    else:
        continue
    c = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(c))
    import datetime
    c = datetime.datetime.strptime(c, "%Y-%m-%d %H:%M:%S")
    print(c)
    '''
    '''
    t = tt['created_at']
    from datetime import datetime, timedelta
    current_year = datetime.today().strftime("%Y")
    min = int(re.findall('\d+', t)[0])
    if u'秒' in t:
        s = (datetime.now() - timedelta(seconds=min))
        print(s)
    elif u'分钟' in t:
        s = (datetime.now() - timedelta(minutes=min))
        print(s)
    elif u'小时' in t:
        s = (datetime.now() - timedelta(hours=min))

    elif u'天' in t:
        s = (datetime.now() - timedelta(days=min))
        print(s)
    else:
        t += ", " + current_year
        s = datetime.strptime(t, "%m-%d, %Y")
    print(s)#精确到小数点后
    print(str(s).split('.')[0])#去除小数点
    '''
import time
import datetime

def conv_time(t):
    min = re.findall('\d+', t)[0]
    if u'分钟' in t:
        c = time.time() - int(min) * 60

    elif u'小时' in t:
        c = time.time() - int(min)*60*60

    elif u'天' in t:
        c = time.time() - int(min)*60*60*24
    else:
        return None
    c = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(c))
    c = datetime.datetime.strptime(c, "%Y-%m-%d %H:%M:%S")
    return str(int(time.mktime(c.timetuple()))) + "000"
后面看到了datetime的timedelta, 所以代码改成了这样

from datetime import datetime, timedelta

current_year = datetime.today().strftime("%Y")

def conv_time(t):
    min = int(re.findall('\d+', t)[0])
    if u'秒' in t:
        s = (datetime.now() - timedelta(seconds=min))
    elif u'分钟' in t:
        s = (datetime.now() - timedelta(minutes=min))

    elif u'小时' in t:
        s = (datetime.now() - timedelta(hours=min))

    elif u'天' in t:
        s = (datetime.now() - timedelta(days=min))
    else:
        t += ", " + current_year
        s = datetime.strptime(t, "%m-%d, %Y")
    return str(int(time.mktime(s.timetuple()))) + "000"
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值