import pytchat
chat = pytchat.create(video_id="uIx8l2xlYVY")
while chat.is_alive():
for c in chat.get().sync_items():
print(f"{c.datetime} [{c.author.name}]- {c.message}")
我用这个pytchat脚本来获取youtube的实时聊天。通常情况下,它可以正常工作,但有时我会出错:
Traceback (most recent call last):
File "pytchat.py", line 14, in <module>
for c in chat.get().sync_items():
AttributeError: 'list' object has no attribute 'sync_items'
之后,它停止工作,我需要手动重新启动它。
你知道我怎么解决这个问题吗?如果没有更好的选择的话,也许是某种自动重启?
发布于 2022-09-18 22:42:14
您可以使用try-以外块来捕获错误并重新启动脚本。
import pytchat
chat = pytchat.create(video_id="uIx8l2xlYVY")
while chat.is_alive():
try:
for c in chat.get().sync_items():
print(f"{c.datetime} [{c.author.name}]- {c.message}")
except AttributeError:
print("Error occurred. Restarting...")
chat = pytchat.create(video_id="uIx8l2xlYVY")
https://stackoverflow.com/questions/73766835
复制相似问题