python - 在输出python中寻找字符串?

标签 python youtube google-api

基本上,我想通过在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/

相关文章:

youtube - 如何从 Youtube 视频禁用右键单击选项和 youtube 水印标志

php - 嵌入式 Youtube 视频的响应式布局问题

javascript - NodeJS 谷歌云/视觉 : methods does not perform anything

javascript - 使用 Angular 2 实现 Google Calendar API

python - 如何在python中按天排序基于键的字典

python - 具有小数位频率的 FFT - 如何量化频率的权重相等?

Python,无法让 assert_raises 正常工作

http - Youtube 发送错误的过期日期

c# - 没有浏览器的谷歌驱动 OAuth2

python - 嵌套 SELECT 查询有时会停止