基本上,我想通过在Google API的响应中查找字符串来使python检查youtube channel 是否处于直播状态。如果该字符串存在,则该 channel 处于 Activity 状态,否则不存在。
我现在的代码是
from urllib.request import urlopen
import json, time, codecs, re
url = "https://www.googleapis.com/youtube/v3/search?part=snippet&channelId=CHANNELID&type=video&eventType=live&key=KEY"
response = urlopen(url)
reader = codecs.getreader("utf-8")
data = json.load(reader(response))
print (data)
我如何添加一些内容来检查响应中是否包含“'liveBroadcastContent':'live”并将值设置为true?谢谢。
最佳答案
使用正则表达式搜索您提到的模式:
if 'livebroadcastcontent' in data:
match=re.match(r'live', data)
if match==true:
print("its live")
关于python - 在输出python中寻找字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44788385/