Python 如何使用youtube数据api检查creativecommons视频?我有以下代码,如果视频是CreativeCommons,如何打印true?

Python 如何使用youtube数据api检查creativecommons视频?我有以下代码,如果视频是CreativeCommons,如何打印true?,python,json,youtube-api,urllib2,youtube-data-api,Python,Json,Youtube Api,Urllib2,Youtube Data Api,我正在尝试使用YouTube数据API下载一些creative commons视频,但我对这一点非常陌生,所以我被卡住了。如何前进?我想在JSON文件中找到“license”:“creativeCommon”,如果为true,则打印true import urllib.request as urllib2 import json response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/videos?id=gwLej8he

我正在尝试使用YouTube数据API下载一些creative commons视频,但我对这一点非常陌生,所以我被卡住了。如何前进?我想在JSON文件中找到“license”:“creativeCommon”,如果为true,则打印true

import urllib.request as urllib2
import json
response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/videos?id=gwLej8heN5c&part=status&key=MY_KEY')
data = list(json.load(response))



@答案是正确的,只要您考虑到json.load使用read函数,这意味着一旦读取了数据,就不能再读取。在这种情况下,它将返回空字节字符串

此代码通过在末尾打印True来工作

response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/videos?id=gwLej8heN5c&part=status&key=MY_KEY')
for item in json.load(response)['items']: print(item['status']['license'] == 'creativeCommon')
另外,在原始示例和错误示例中,在保存到数据时使用了list(json.load(response))。这意味着,您不会得到整个json,而只得到键。因此,对于您的情况,我建议不要更改对列表的响应。但由于在本例后面并没有使用数据变量,所以它实际上并没有改变结果。但是,如果您有更多的信息要检查/保存,这可能很重要

此外,在本例中,由于json.load()使用了read函数(不能多次使用),因此需要保存整个json,然后从中读取。代码将是:

response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/videos?id=gwLej8heN5c&part=status&key=MY_KEY')
data = json.load(response)
for item in data['items']: print(item['status']['license'] == 'creativeCommon')
如果您尝试阅读得到的响应,您可以看到这一点。您可以使用以下代码对其进行测试:

response = urllib2.urlopen('https://www.googleapis.com/youtube/v3/videos?id=gwLej8heN5c&part=status&key=MY_KEY')
print(response.read())
print(response.read())
在这种情况下,结果将是:

b'{\n "kind": "youtube#videoListResponse",\n "etag": "\\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/3jdRB-NXSAfUQj7e_FmBbivkK1o\\"",\n "pageInfo": {\n  "totalResults": 1,\n  "resultsPerPage": 1\n },\n "items": [\n  {\n   "kind": "youtube#video",\n   "etag": "\\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/NUd32t1_moLGAwVuu-ZujlkaiWM\\"",\n   "id": "gwLej8heN5c",\n   "status": {\n    "uploadStatus": "processed",\n    "privacyStatus": "public",\n    "license": "creativeCommon",\n    "embeddable": true,\n    "publicStatsViewable": true\n   }\n  }\n ]\n}\n'
b''

在第一种情况下,结果是字节字符串形式的json,而第二种是空字节字符串。因此,当您尝试在同一个响应元素上使用多个json.load()时,您将得到JSONDecodeError(与注释中的相同),因为第二次没有要解析的json

>代码> jj.Load(响应)[项目]中的项目:打印(项目[状态] ] [许可证]='CalvestEnMeMon ')/>代码>考虑使用一个库,并检查<代码>状态>许可证>代码>,而不是自行解析。@好振动,您的代码返回错误。看,我不想用图书馆
b'{\n "kind": "youtube#videoListResponse",\n "etag": "\\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/3jdRB-NXSAfUQj7e_FmBbivkK1o\\"",\n "pageInfo": {\n  "totalResults": 1,\n  "resultsPerPage": 1\n },\n "items": [\n  {\n   "kind": "youtube#video",\n   "etag": "\\"Bdx4f4ps3xCOOo1WZ91nTLkRZ_c/NUd32t1_moLGAwVuu-ZujlkaiWM\\"",\n   "id": "gwLej8heN5c",\n   "status": {\n    "uploadStatus": "processed",\n    "privacyStatus": "public",\n    "license": "creativeCommon",\n    "embeddable": true,\n    "publicStatsViewable": true\n   }\n  }\n ]\n}\n'
b''