下面講自己在某論壇**上遇到的時間格式解析
在下面的所有時間操作中,都是將時間轉換成標準的格式 %y-%m-%d %h:%m:%s 示例:2018-07-26 18:56:42
在示例**中會出現 s_time 這個字串是我們提取出來的字串,需要做處理的字串。result_time 是我們處理的結果。
1、s_time = "2017-06-15"
2、s_time = "6天前"if re.findall(r'\d-\d-\d', s_time):
result_time = time.strftime("%y-%m-%d %h:%m:%s", time.strptime(s_time, "%y-%m-%d"))
3、s_time = "昨天 18:03"elif u'天前' in s_time:
days = re.findall(u'(\d+)天前', s_time)[0]
result_time = (datetime.now() - timedelta(days=int(days))).strftime("%y-%m-%d %h:%m:%s")
4、s_time = "28分鐘前"elif u'昨天' in s_time:
last_time = re.findall(r'.*?(\d:\d)', s_time)[0]
days_ago = datetime.now() - timedelta(days=int(1))
y_m_d = str(days_ago.year) + '-' + str(days_ago.month) + '-' + str(days_ago.day)
_time = y_m_d + ' ' + last_time
result_time = time.strftime("%y-%m-%d %h:%m:%s", time.strptime(_time, "%y-%m-%d %h:%m"))
5、s_time = "06-29"elif u'分鐘前' in s_time:
minutes = re.findall(u'(\d+)分鐘', s_time)[0]
minutes_ago = (datetime.now() - timedelta(minutes=int(minutes))).strftime("%y-%m-%d %h:%m:%s")
result_time = minutes_ago
6、s_time = "1小時前"elif re.findall(r'\d-\d', s_time) and len(s_time) <= 5:
now_year = str(datetime.now().year)
_time = now_year + '-' + s_time
result_time = time.strftime("%y-%m-%d %h:%m:%s", time.strptime(_time, "%y-%m-%d"))
7、s_time = "1532573387"elif u'小時前' in s_time:
hours = re.findall(u'(\d+)小時前', s_time)[0]
hours_ago = (datetime.now() - timedelta(hours=int(hours))).strftime("%y-%m-%d %h:%m:%s")
result_time = hours_ago
以上就是某論壇基本的時間格式。最後貼出完整**。elif re.findall('\d',s_time)[0]:
_t = int(s_time)
_time = time.localtime(int(_t))
result_time = time.strftime("%y-%m-%d %h:%m:%s", _time)
下面是某次爬蟲遇到的結果。def parse_time(self,s_time):
result_time = ''
# 1、2017-06-15
if re.findall(r'\d-\d-\d', s_time):
result_time = time.strftime("%y-%m-%d %h:%m:%s", time.strptime(s_time, "%y-%m-%d"))
# 6天前
elif u'天前' in s_time:
days = re.findall(u'(\d+)天前', s_time)[0]
result_time = (datetime.now() - timedelta(days=int(days))).strftime("%y-%m-%d %h:%m:%s")
# 昨天 18:03
elif u'昨天' in s_time:
last_time = re.findall(r'.*?(\d:\d)', s_time)[0]
days_ago = datetime.now() - timedelta(days=int(1))
y_m_d = str(days_ago.year) + '-' + str(days_ago.month) + '-' + str(days_ago.day)
_time = y_m_d + ' ' + last_time
result_time = time.strftime("%y-%m-%d %h:%m:%s", time.strptime(_time, "%y-%m-%d %h:%m"))
# 28分鐘前
elif u'分鐘前' in s_time:
minutes = re.findall(u'(\d+)分鐘', s_time)[0]
minutes_ago = (datetime.now() - timedelta(minutes=int(minutes))).strftime("%y-%m-%d %h:%m:%s")
result_time = minutes_ago
# 06-29
elif re.findall(r'\d-\d', s_time) and len(s_time) <= 5:
now_year = str(datetime.now().year)
_time = now_year + '-' + s_time
result_time = time.strftime("%y-%m-%d %h:%m:%s", time.strptime(_time, "%y-%m-%d"))
# 1小時前
elif u'小時前' in s_time:
hours = re.findall(u'(\d+)小時前', s_time)[0]
hours_ago = (datetime.now() - timedelta(hours=int(hours))).strftime("%y-%m-%d %h:%m:%s")
result_time = hours_ago
return result_time
Python時間格式
關於python時間格式總結 1 時間戳 import time print time.time 直接輸出時間戳 1481357223 利用time時間模組轉換,格式化時間戳為本地的時間 time.localtime time.time time.struct time tm year 2016,t...
python時間格式
1.日期輸出格式化 datetime string import datetime now datetime.datetime.now now.strftime y m d h m s 輸出 2015 04 07 19 11 21 strftime是datetime類的例項方法。2.日期輸出格式化 ...
python 的時間格式
今天用到python 的時間格式了,寫下來留給自己,也分享給大家 取得時間相關的資訊的話,要用到python time模組,python time模組裡面有很多非常好用的功能,你可以去官方 文件了解下,要取的當前時間的話,要取得當前時間的時間戳,時間戳好像是1970年到現在時間相隔的時間。你可以試下...