python爬虫中国标准时间转换为格林时间

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/ACBattle/article/details/82911991

做爬虫的时候发现返回的时间格式无法插入mysql,需要做一步转化,没找到简单的转换方式,就只能手动转。如果有其他简单的方式,求教!

中国标准时间格式:Sun Sep 30 17:12:21 +0800 2018
插入mysql的时间格式:2018-09-03 17:12:21

import time
def trans_format(time_string, from_format, to_format='%Y.%m.%d %H:%M:%S'):
    time_struct = time.strptime(time_string, from_format)
    times = time.strftime(to_format, time_struct)
    return times
time_string = 'Feb 01 2017 23:00:12' # 中国标准时间更长,更全面,手动就是纯粹的截取字符串
#--------------------------------------------------------------------------------
# pubtime = 'Sun Sep 30 17:12:21 +0800 2018'
# time_string = pubtime[4:9]+' '+pubtime[26:30] + ' ' + pubtime[11:19] 4-9是把前面的星期抹去,26-30是把年提前,11-19是把具体时间接上
#-------------------------------------------------------------------------------
times = trans_format(time_string, '%b %d %Y %H:%M:%S', '%Y-%m-%d %H:%M:%S')
print times

猜你喜欢

转载自blog.csdn.net/ACBattle/article/details/82911991