小弟我有一支python程式每三小時會執行一次,網路撈資料並產生檔案,但只要在晚上22點執行撈19-22的資料就會失敗。
先上程式碼
1.先判定時間區間
from datetime import datetime, timedelta
current_time = datetime.now()
time_ranges = [
(7, 10),
(10, 13),
(13, 16),
(16, 19),
(19, 22),
(22, 1),
(1, 4),
(4, 7)
]
a = current_time.strftime("%Y-%m-%d %H:%M:%S")
file_name = ""
# 目前時間比對時間範圍
for start_hour, end_hour in time_ranges:
start_time = current_time.replace(hour=start_hour, minute=0, second=0, microsecond=0)
end_time = current_time.replace(hour=end_hour, minute=0, second=0, microsecond=0)
if start_hour > end_hour:
if current_time.hour < start_hour:
end_time += timedelta(days=-1) # 當前時間在第二天的時間範圍內
else:
end_time += timedelta(days=1) # 當前時間在第一天的時間範圍內
#檔名命名
if start_time <= current_time < end_time:
start_time -= timedelta(hours=3)
end_time -= timedelta(hours=3)
file_name = (
start_time.strftime("%Y-%m-%d-%H-%M-%S") + "-"
+ end_time.strftime("%Y-%m-%d-%H-%M-%S") + ".xlsx"
)
break
print("檔案名稱:", file_name)
2.到網頁撈時間區間內的資料
start_time = current_time.replace(hour=start_hour, minute=0, second=0, microsecond=0)
end_time = current_time.replace(hour=end_hour, minute=0, second=0, microsecond=0)
specific_date = datetime.datetime(1970, 1, 1, 0, 0, 0)
# 因為時差要先-11小時
start_time = start_time - datetime.timedelta(hours=11)
end_time = end_time - datetime.timedelta(hours=11)
# print(start_time,end_time)
# 计算时间差
start_time = start_time - specific_date
end_time = end_time - specific_date
# 获取时间差的总秒数
start_time = int(start_time.total_seconds())
end_time = int(end_time.total_seconds())
# print(start_time,end_time)
try:
url_string=f'http://{ip}:9999/api/detection/ex?start_time={start_time}&end_time={end_time}&result=ALL'
# print(url_string)
except:
print('有問題')
3.正常的數值
1704355200-1704366000
失敗的數值
1704279600-1704204000
->前面的數值是正確的 但後面的1704204000會固定出現(不是我要的)
目前檔案產生都正常,名稱也正確,網頁都有正常運作,但在晚上22點就是撈不到資料。
附上log
正常
Jan 3 19:05:01 mis-pyserver CRON[4856]: (mis) CMD ( python3.10 /home/mis/Desktop/py/ccd_top3_bad_report_day/top3.py)
Jan 3 19:14:12 mis-pyserver postfix/pickup[4641]: B2A99260630: uid=1000 from=<mis>
Jan 3 19:14:12 mis-pyserver postfix/cleanup[5052]: B2A99260630: message-id=<20240103111412.B2A99260630@mis-pyserver>
Jan 3 19:14:12 mis-pyserver postfix/qmgr[1621]: B2A99260630: from=<mis@mis-pyserver>, size=84592, nrcpt=1 (queue active)
Jan 3 19:14:12 mis-pyserver postfix/local[5054]: B2A99260630: to=<mis@mis-pyserver>, orig_to=<mis>, relay=local, delay=0.05, delays=0.04/0.01/0/0, dsn=2.0.0, status=sent (delivered to m>
Jan 3 19:14:12 mis-pyserver postfix/qmgr[1621]: B2A99260630: removed
失敗
Jan 3 22:05:01 mis-pyserver CRON[5159]: (mis) CMD ( python3.10 /home/mis/Desktop/py/ccd_top3_bad_report_day/top3.py)
Jan 3 22:07:15 mis-pyserver postfix/pickup[5154]: 2520B260630: uid=1000 from=<mis>
Jan 3 22:07:15 mis-pyserver postfix/cleanup[5354]: 2520B2`�=`�=a�>60630: message-id=<20240103140715.2520B260630@mis-pyserver>
Jan 3 22:07:15 mis-pyserver postfix/qmgr[1621]: 2520B260630: from=<mis@mis-pyserver>, size=19139, nrcpt=1 (queue active)
Jan 3 22:07:15 mis-pyserver postfix/local[5356]: 2520B260630: to=<mis@mis-pyserver>, orig_to=<mis>, relay=local, delay=0.08, delays=0.06/0.01/0/0.01, dsn=2.0.0, status=sent (delivered t>
Jan 3 22:07:15 mis-pyserver postfix/qmgr[1621]: 2520B260630: removed
from datetime import datetime
import time
# 取得當下日期時間字串,例如: "2023-03-20-22-30-00"
def get_now_datetime_str():
now = datetime.now()
return now.strftime("%Y-%m-%d-%H-%M-%S")
# 將字串 "2023-03-20-22-30-00" 轉為 timestamp "1679359553051"
def datetime_str_to_timestamp(s):
#s = "2023-03-20-22-30-00"
return int(time.mktime(datetime.strptime(s, "%Y-%m-%d-%H-%M-%S").timetuple()))
# 將 timestamp 轉為 日期時間
def timestamp_to_datetime(st):
#st = "1679359553051"
return datetime.fromtimestamp(st)
current_time_str = get_now_datetime_str()
# print(current_time_str)
end_time_str = current_time_str[:11]+current_time_str[11:13]+'-00-00'
end_time = datetime_str_to_timestamp(end_time_str)
start_time = end_time -(60*60*3)
start_time_str = timestamp_to_datetime(start_time).strftime("%Y-%m-%d-%H-%M-%S")
print('start_time',start_time,start_time_str)
print('end_time',end_time,end_time_str)
file_name=f"{start_time_str}-{end_time_str}.xlsx"
print("檔案名稱:", file_name)
ip ='1.2.3.4'
url_string=f'http://{ip}:9999/api/detection/ex?start_time={start_time}&end_time={end_time}&result=ALL'
print(url_string)
Output:
start_time 1704423600 2024-01-05-11-00-00
end_time 1704434400 2024-01-05-14-00-00
檔案名稱: 2024-01-05-11-00-00-2024-01-05-14-00-00.xlsx
http://1.2.3.4:9999/api/detection/ex?start_time=1704423600&end_time=1704434400&result=ALL
本文的回覆貼源碼沒法縮進,只好貼在回答區了。
控制何時執行python腳本的部份依樓主描述應該是沒問題,那麼問題就出在timestamp的部份,我這邊用一個笨方法給您參考,大概邏輯是這樣的: