我有一个脚本,将刮Facebook帖子在一个while循环。但为了防止被Facebook封杀,我更喜欢run(self)
只在特定时间运行,并在1小时后停止。我怎样才能做到呢?我看到有人发帖说,在特定的时间启动一个功能,但在特定的一个小时后不停止它。
在特定时间启动函数的代码:
import schedule
import time
def job(t):
print ("I'm working...", t)
return
schedule.every().day.at("18:44").do(job,'It is 01:00')
while True:
schedule.run_pending()
time.sleep(60) # wait one minute
脚本代码
def run(self):
while True:
try:
wanted = "Pecahan setiap negeri (Kumulatif):" # wanted post
for post in get_posts("myhealthkkm", pages=5):
if post.get("post_text") is not None and wanted in post.get("post_text"):
# print("Found", t)
listposts.append(post.get("post_text"))
# append until 3 page finish then go here
time.sleep(1)
print(listposts)
global listView
if listposts != 0:
listView = listposts.copy()
print(listView)
listposts.clear()
except exceptions.TemporarilyBanned:
print("Temporarily banned, sleeping for 10m")
time.sleep(600)
我希望run()
函数仅在下午3点执行,并在1小时后停止,即每天下午4点。我怎样才能达到这个目标?由于
由于@larsks的响应,我试图在函数内放置时间检查,如果达到3600second
,则退出循环,这等于1小时。
import schedule
import time
def job():
program_starts = time.time()
while True:
now = time.time()
timeframe = now - program_starts
print("It has been {0} seconds since the loop started".format(timeframe))
if timeframe > 3600:
break
schedule.every().day.at("19:20:30").do(job)
while True:
schedule.run_pending()
#time.sleep(60) # wait one minute