1

I have an issue, i am trying to make Python search for videos through the Youtube API, a part of the code doesn't work. When i try to get the tags of the video, it gives me an error.

So far i have used the example from Google "search_by_keyword". It has worked well. I have however changed it a little. What i want is Python to give me video IDs with the tags in that video. I tried debugging, by making it print the whole "snippet" part of the table, it appears that "tags" are not found at all in "snippet"

only a bit of the code have been added, the rest is working find.

for search_result in search_response.get("items", []):
 if search_result["id"]["kind"] == "youtube#video":
   videos.append("%s (%s)" % (search_result["snippet"]["tags"],
                              search_result["id"]["videoId"]))
print ("Videos:\n", "\n".join(videos), "\n")

At

videos.append("%s (%s)" % (search_result["snippet"]["tags"]

i expect it here to give me the tags of the video, but it only comes up with a traceback error "keyerror: tags".

Trying to make it print the whole "snippet" part, gives me everything but "tags" AND "categoryid"

I think the question comes down to: Where do i find the "tags" element in the data table?

1
  • Can you share what error you have encountered when you are trying to run your code? Commented Mar 27, 2019 at 8:03

1 Answer 1

2

As per Google's docs (https://developers.google.com/youtube/v3/docs/search#resource) the JSON object snippet obtained from Search endpoint has no tags member. Therefore, your code getting a KeyError exception exhibits correct behavior.

For reaching the tags property of a video resource (https://developers.google.com/youtube/v3/docs/videos#snippet.tags[]) you have to make a separate API call for each video you're interested in on Videos endpoint (https://developers.google.com/youtube/v3/docs/videos/list).

Addendum: you may alleviate calling the API several times on Videos endpoint for the videos you're interested in by specifying those videos -- in one API call only -- to the parameter id as a comma-separated list of video ids (https://developers.google.com/youtube/v3/docs/videos/list#id).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.