首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用pytchat获取youtube实时聊天

用pytchat获取youtube实时聊天
EN

Stack Overflow用户
提问于 2022-09-18 22:37:06
回答 1查看 89关注 0票数 2
代码语言:javascript
运行
复制
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的实时聊天。通常情况下,它可以正常工作,但有时我会出错:

代码语言:javascript
运行
复制
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'

之后,它停止工作,我需要手动重新启动它。

你知道我怎么解决这个问题吗?如果没有更好的选择的话,也许是某种自动重启?

EN

回答 1

Stack Overflow用户

发布于 2022-09-18 22:42:14

您可以使用try-以外块来捕获错误并重新启动脚本。

代码语言:javascript
运行
复制
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")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/73766835

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档