As the title suggests I am trying to get the "videoId" from a YouTube video.
Currently this is my code, it can be replicated.
import requests
channel_id = "UCfjTZZ3iqy24oSg-bmi2waw"
api_key = ""
api_url = f"https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={channel_id}&maxResults=1&order=date&type=video&key={api_key}"
request = requests.get(api_url).text
print(request)
When doing this method it returns me this: result from requests.get(api_url).text
If I were to do this
print(request["videoId"])
It would return me this
TypeError: string indices must be integers, not 'str'
Am I just doing it wrong? Even if I try requesting a higher key it returns the same error message.
CodePudding user response:
You need to parse the received JSON and to precise the path of the entry you are looking for, see the code below:
import requests, json
channel_id = "UCfjTZZ3iqy24oSg-bmi2waw"
api_key = ""
api_url = f"https://www.googleapis.com/youtube/v3/search?part=snippet&channelId={channel_id}&maxResults=1&order=date&type=video&key={api_key}"
request = json.loads(requests.get(api_url).text)
print(request["items"][0]["id"]["videoId"])