且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python - *** API v3 - 如何仅获取视频 ID?

更新时间:2023-02-20 23:25:10

您可以在此处尝试查询:

You can try your query here:

https://developers.google.com/***/v3/docs/search/list#try-it

我相信以下查询是您只想搜索和检索视频 ID 的方式:

I believe that the following query is the way in what you want to search and retrieve only the video id:

https://www.googleapis.com/***/v3/search?part=id&q={NAME}&type=video&fields=items%2Fid&key={YOUR_API_KEY}

https://www.googleapis.com/***/v3/search?part=id&q={NAME}&type=video&fields=items%2Fid&key={YOUR_API_KEY}

例如,如果 {NAME} 是 psy,则此调用将返回以下数据,您可以从中检索其中一项的 videoId;

If for example {NAME} is psy, then this call returns the following data, from where you can retrieve the videoId of one of the items;

{
 "items": [
  {
   "id": {
    "kind": "***#video",
    "videoId": "9bZkp7q19f0"
   }
  },
  {
   "id": {
    "kind": "***#video",
    "videoId": "Ecw4O5KgvsU"
   }
  },
  {
   "id": {
    "kind": "***#video",
    "videoId": "o443b2rfFnY"
   }
  },
  {
   "id": {
    "kind": "***#video",
    "videoId": "WOyo7JD7hjo"
   }
  },
  {
   "id": {
    "kind": "***#video",
    "videoId": "QZmkU5Pg1sw"
   }
  }
 ]
}

如果修改python客户端库中包含的示例:

if you modify the example included in the python client library:

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"