参考链接:https://www.cnblogs.com/yingsong/p/5613735.html
Python3.7
第三方模块支持: pip install python-dateutil
要转换的格式:Wed Apr 08 23:44:57 2015 -0500 or 2017-07-31 20:18:00 +0530
目标格式是:2017-07-31 20:18:00
import requests
from bs4 import BeautifulSoup
from dateutil.parser import parse
from src.lib.proxypool import proxies, headers
def calc_mon(tm_year, tm_mon):
if tm_mon > 12:
new_tm_year = tm_year + 1
new_tm_mon = tm_mon - 12
return new_tm_year, new_tm_mon
else:
return tm_year, tm_mon
def calc_day(tm_mon, tm_mday):
"""
计算每个月有多少天,是否大于规定的31或者30,若大于则月份加1
:param tm_mon:
:param tm_mday:
:return:
"""
if tm_mon in [1, 3, 5, 7, 8, 10, 12]:
if tm_mday > 31:
new_tm_mon = tm_mon + 1
new_tm_mday = tm_mday - 31
return new_tm_mon, new_tm_mday
else:
return tm_mon, tm_mday
elif tm_mon in [2, 4, 6, 9, 11]:
if tm_mday > 30:
new_tm_mon = tm_mon + 1
new_tm_mday = tm_mday - 30
return new_tm_mon, new_tm_mday
else:
return tm_mon, tm_mday
def calc_hour(tm_mday, tm_hour, diff_hour):
tm_hour = tm_hour + diff_hour # 转换成北京时区
if tm_hour >= 24:
new_tm_mday = tm_mday + 1
new_tm_hour = tm_hour - 24
return new_tm_mday, new_tm_hour
else:
return tm_mday, tm_hour
def calc_min(tm_hour, tm_min, diff_min):
if tm_min + diff_min >= 60:
new_tm_hour = tm_hour + 1
new_tm_min = (tm_min + diff_min) - 60
return new_tm_hour, new_tm_min
else:
new_tm_min = tm_min + diff_min
return tm_hour, new_tm_min
def analysis_conditions(utc_diff):
if "-" in utc_diff:
utc_diff_time = utc_diff.strip("-")
# print(utc_diff_time[:2], utc_diff_time[2:])
diff_hour = int(utc_diff_time[:2])
diff_min = int(utc_diff_time[2:])
return diff_hour + 8, diff_min
else:
bj_utc_hour = int("0800"[:2])
bj_utc_min = int("0800"[2:])
utc_diff_time = utc_diff.strip("+")
utc_diff_hour = int(utc_diff_time.strip("+")[:2])
utc_diff_min = int(utc_diff_time.strip("+")[2:])
# print(bj_utc_time, utc_diff_time)
if bj_utc_hour > utc_diff_hour:
diff_min = bj_utc_min - utc_diff_min
diff_hour = bj_utc_hour - utc_diff_hour
if '-' in str(diff_min):
# print("diff_min ", int(str(diff_min).strip('-')))
# print(diff_hour, diff_min)
return diff_hour - 1, int(str(diff_min).strip('-'))
else:
return diff_hour, diff_min
else:
diff_min = bj_utc_min - utc_diff_min
diff_hour = bj_utc_hour - utc_diff_hour
# print(diff_hour, diff_min)
return diff_hour, diff_min
def to_beijing_time(str_time):
struct_time = parse(str_time)
utc_diff = str_time.split(" ")[-1]
diff_hour, diff_min = analysis_conditions(utc_diff)
# print(diff_hour, diff_min)
new_tm_hour, new_tm_min = calc_min(struct_time.hour, struct_time.minute, diff_min)
new_tm_mday, new_tm_hour = calc_hour(struct_time.day, new_tm_hour, diff_hour)
new_tm_mon, new_tm_mday = calc_day(struct_time.month, new_tm_mday)
new_tm_year, new_tm_mon = calc_mon(struct_time.year, new_tm_mon)
if len(str(new_tm_hour)) == 1:
tm_hour = "0" + str(new_tm_hour)
else:
tm_hour = str(new_tm_hour)
if len(str(new_tm_min)) == 1:
tm_min = "0" + str(new_tm_min)
else:
tm_min = str(new_tm_min)
# print("Final time ", str(new_tm_year) + "-" + str(new_tm_mon) + "-" + str(
# new_tm_mday) + " " + tm_hour + ":" + tm_min + ":" + str(struct_time.second))
return str(new_tm_year) + "-" + str(new_tm_mon) + "-" + str(
new_tm_mday) + " " + tm_hour + ":" + tm_min + ":" + str(struct_time.second)
def googlesource(url):
"""
"https://android.googlesource.com/platform/frameworks/av/+/2434839bbd168469f80dd9a22f1328bc81046398"
:param url:
:return:
"""
req = requests.get(url=url, proxies=proxies)
soup = BeautifulSoup(req.text, 'lxml')
# print(soup)
title = soup.findAll(attrs={"class": "u-monospace Metadata"})
try:
report_time = title[0].findAll('td')[3].text
fix_time = title[0].findAll('td')[5].text
rt = to_beijing_time(report_time)
ft = to_beijing_time(fix_time)
# print("------googlesource-----", rt, ft)
return rt, ft
except:
return "", ""
def codeaurora(url):
"""
"https://source.codeaurora.org/quic/la/platform/vendor/qcom-opensource/wlan/qcacld-2.0/commit/?id=71331327ac389bff7d5af2707c4325e5b7949013",
:return:
"""
req = requests.get(url=url, proxies=proxies)
soup = BeautifulSoup(req.text, 'lxml')
title = soup.findAll(attrs={"class": "right"})
# print(title)
try:
report_time = title[2].text
fix_time = title[3].text
# print(report_time, fix_time)
rt = to_beijing_time(report_time)
ft = to_beijing_time(fix_time)
# print("------codeaurora-----", rt, ft)
return rt, ft
except:
return "", ""
# if __name__ == '__main__':
# codeaurora()
# googlesource()
def kernel(url):
req = requests.get(url=url, headers=headers, proxies=proxies)
soup = BeautifulSoup(req.text, 'lxml')
# print(soup)
title = soup.findAll(attrs={"class": "right"})
# print(title[2].text)
# print(title[3].text)
try:
report_time = title[2].text
fix_time = title[3].text
# print(report_time, fix_time)
rt = to_beijing_time(report_time)
ft = to_beijing_time(fix_time)
# print("------kernel-----", rt, ft)
return rt, ft
except:
return "", ""
def req_time(url):
if "codeaurora" in url:
rt, ft = codeaurora(url)
return rt, ft
elif "googlesource" in url:
rt, ft = googlesource(url)
return rt, ft
elif "kernel" in url:
rt, ft = kernel(url)
return rt, ft