提问人:Hollow 提问时间:11/16/2023 更新时间:11/16/2023 访问量:25
python脚本读取youtube聊天一段时间后无缘无故停止工作
python script reading youtube chat stops working for no reason after a while
问:
基本上,我有这个python脚本,它使用pytchat读取YouTube聊天并保存以/topic开头的消息。它通常工作半小时,最终关闭而不会出现任何错误(我尝试通过 cmd 运行它,但仍然没有错误,它只是停止了)。我如何让它 24/7 全天候工作,我的代码中是否有任何错误?
# -*- coding: utf-8 -*-
import pytchat
video_id = "iAa3fFY1Hg0"
chat = pytchat.create(video_id)
file_path = "C:/Users/80nin/ai/Assets/chat_log.txt"
def write_to_file(author, message):
cleaned_message = message[len("/topic"):].strip()
with open(file_path, "a", encoding="utf-8") as file:
file.write(f"{author.name} - {cleaned_message}\n")
while True:
try:
for c in chat.get().sync_items():
print(f"{c.author.name} - {c.message}")
if c.message.startswith("/topic"):
write_to_file(c.author, c.message)
except pytchat.ChatDataFinished:
print("Chat data finished, waiting for new messages...")
time.sleep(10) # Adjust the wait time as needed
except Exception as e:
print(f"An error occurred: {str(e)}")
time.sleep(10) # Wait before trying again to avoid continuous errors
我尝试通过 cmd 运行它,但仍然没有错误,它只是停止了
答: 暂无答案
评论
write_to_file