如何仅获取视频的视频ID?据我所知,我应该使用字段,但是我不知道它们是如何工作的。我的代码:
service = build('youtube', 'v3', developerKey = api_key)
request = service.search().list(q = name, part='id',fields = *what should I type here*, maxResults = 1, type = 'video').execute()
“名称”是搜索词变量。我是从包含名称列表的文件中获得的。通过使用此代码,我可以获得不需要的信息。就像我说的,我只需要视频ID。
最佳答案
您可以在这里尝试查询:
https://developers.google.com/youtube/v3/docs/search/list#try-it
我相信以下查询是您要搜索和仅检索视频ID的方式:
https://www.googleapis.com/youtube/v3/search?part=id&q= {NAME}&type = video&fields = items%2Fid&key = {YOUR_API_KEY}
例如,如果{NAME}为psy,则此调用返回以下数据,您可以从中检索其中一项的videoId。
{
"items": [
{
"id": {
"kind": "youtube#video",
"videoId": "9bZkp7q19f0"
}
},
{
"id": {
"kind": "youtube#video",
"videoId": "Ecw4O5KgvsU"
}
},
{
"id": {
"kind": "youtube#video",
"videoId": "o443b2rfFnY"
}
},
{
"id": {
"kind": "youtube#video",
"videoId": "WOyo7JD7hjo"
}
},
{
"id": {
"kind": "youtube#video",
"videoId": "QZmkU5Pg1sw"
}
}
]
}
如果您修改python客户端库中包含的示例:
https://developers.google.com/api-client-library/python/
您可以通过以下方式进行操作:
search_response = service.search().list(
q="google",
part="id",
type="video",
fields="items/id"
).execute()
videos = []
for search_result in search_response.get("items", []):
videos.append("%s" % (search_result["id"]["videoId"]))
print "Videos:\n", "\n".join(videos), "\n"
关于python - Python-YouTube API v3-如何仅获取视频ID?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/14528958/