selenium_elm_fengshen/test.py

35 lines
1.3 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import re
def parse_online_time(online_time_str):
# 如果字符串中同时包含小时和分钟
if "小时" in online_time_str and "分钟" in online_time_str:
match = re.search(r'(\d+)小时(\d+)分钟', online_time_str)
if match:
hours, minutes = match.groups()
return int(hours) * 60 + int(minutes)
# 如果字符串中只包含小时
elif "小时" in online_time_str:
match = re.search(r'(\d+)小时', online_time_str)
if match:
hours = match.group(1)
return int(hours) * 60
# 如果字符串中只包含分钟
elif "分钟" in online_time_str:
match = re.search(r'(\d+)分钟', online_time_str)
if match:
minutes = match.group(1)
return int(minutes)
# 如果字符串中既不包含小时也不包含分钟则返回0
else:
return 0
# 测试函数
print(parse_online_time("晚高峰 目标在线时长2小时30分钟目标完单量5")) # 应该返回150
print(parse_online_time("晚高峰 目标在线时长1小时目标完单量5")) # 应该返回60
print(parse_online_time("晚高峰 目标在线时长30分钟目标完单量5")) # 应该返回30
print(parse_online_time("晚高峰 目标完单量5")) # 应该返回0