我不得不在Youtube上获取实时聊天,并选择使用pytchat而不是Youtube API。获取聊天记录没有问题,但速度有点慢
from pytchat import LiveChat
from datetime import datetime
chat = LiveChat(video_id = "36YnV9STBqc")
k=[]
while chat.is_alive():
now2 = datetime.now()
current_time2 = now2.strftime("%H:%M:%S")
print("Current Time =", current_time2,"==========") #A
try:
data = chat.get()
now3 = datetime.now()
current_time3 = now3.strftime("%H:%M:%S")
print("Current Time =", current_time3,"~~~~~~~~~~~~~") #B
for c in data.items:
comment=f"[{c.datetime}-{c.message}]"
k.append(comment) #I need to use k later
now = datetime.now()
current_time = now.strftime("%H:%M:%S")
print(comment,"Current Time =", current_time,"++++++++++++++++")
data.tick()
except KeyboardInterrupt:
chat.terminate()
break
下面显示了17649人观看的视频输出:(B到a需要5秒)
Current Time = 18:49:33 ========== #A
Current Time = 18:49:33 ~~~~~~~~~~~~~ #B
[2020-05-30 18:49:29-hi] Current Time = 18:49:33 ++++++++++++++++ #4 seconds late
[2020-05-30 18:49:32-how are you] Current Time = 18:49:36 ++++++++++++++++
Current Time = 18:49:38 ========== #A
Current Time = 18:49:38 ~~~~~~~~~~~~~ #B
[2020-05-30 18:49:32-so good] Current Time = 18:49:38 ++++++++++++++++ #6 seconds late
下面显示了702观看的视频输出:(B到a至少需要10秒)
Current Time = 18:49:09 ========== #A
Current Time = 18:49:10 ~~~~~~~~~~~~~ #B
[2020-05-30 18:49:06-hellp] Current Time = 18:49:10 ++++++++++++++++
[2020-05-30 18:49:07-love the music] Current Time = 18:49:15 ++++++++++++++++
Current Time = 18:49:20 ========== #A
Current Time = 18:49:20 ~~~~~~~~~~~~~ #B
[2020-05-30 18:49:15-???] Current Time = 18:49:20 ++++++++++++++++
我假设不同的观看量会影响时间?每次聊天都要迟到4到6秒,有可能解决吗?或者这就是Pytchat的工作原理
这是一个规范
Pytchat以与浏览器完全相同的方式获取聊天。 如果您的浏览器显示聊天时间和当前时间,直到秒,您将得到相同的结果
YouTube服务器的响应可能受到观看聊天的人数以及在任何给定时间发布聊天的人数的影响
它需要验证,但正如您所指出的,我猜如果发布了大量聊天帖子,YouTube服务器需要更长的时间来处理这些帖子并返回检索到的聊天
(如果将
data.tick()
注释掉,可能会得到更好的结果。)使用
sync_items()将为您提供适当的实时聊天移动
相关问题 更多 >
编程相关推荐