I have a long list of musicians that I need to extract social media information for on a regular basis, including YouTube. I have populated each musicians' "channel name" as follows:

  • ["taylorswift"],
  • ["rihanna"],
  • ["SiaVEVO"],
  • ["AlessoOfficialVEVO"],
  • etc

To extract the information I use the following code:

for item in list:
yt_user=item[0]
youtube='https://www.googleapis.com/youtube/v3/channels?part=statistics&forUsername='+str(yt_user)+'&key=***key***'
youtube2 = urllib2.urlopen(youtube)
tube = json.load(youtube2)
a=str(tube['items'][0]['statistics']['viewCount'])
b=str(tube['items'][0]['statistics']['subscriberCount'])  
out=[a,b]

However some musician channel pages do not have a "https://www.youtube.com/user/RihannaVEVO" style URL. Instead Jack U has the following "https://www.youtube.com/channel/UCtuGozGshjqqErhHGEmHnJw" style.

Apologies if I have missed something very simple but I can't find any way to alter the Channels.List API call when there isn't a Username but only a channelId.

Thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

Instead of passing the value in the field "forUsername" pass the channel id in the field "id"