1. 程式人生 > >python爬蟲中國標準時間轉換為格林時間

python爬蟲中國標準時間轉換為格林時間

做爬蟲的時候發現返回的時間格式無法插入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