Get ChannelID from Youtube Custom URL

Viewed 5152

Is there any way that I can get the channel ID of a Youtube Channel by having its custom url (using the Youtube API) ?

Example:

A custom url is like:

https://www.youtube.com/onepiece

I want to get its channelID, so that I have the link like:

www.youtube.com/user/OnePieceUK

3 Answers

Elaborating on this answer of mine, I'll note here the following facts:

  1. In YouTube URLs of form https://www.youtube.com/c/NAME or https://www.youtube.com/NAME, NAME is a channel's custom URL. (See this official account from Google support.)

  2. In YouTube URLs of form https://www.youtube.com/user/NAME, NAME is a channel's user name. User names are a legacy feature of the API v3; not every channel has one attached; no channel is required to have one attached. (See this official statement from Google staff from 2013-07-11.)

The two API concepts -- custom URLs and user names -- encompass two different categories.

The public (MIT licensed) Python 3 script youtube-search.py referred by my answer quoted above is able to search the API for custom URLs and respectively query the API for user names:

$ python3 youtube-search.py --custom-url onepiece
UC6LPb3zSebrzU_0Yclpwb4Q

$ python3 youtube-search.py --user-name OnePieceUK
UC6LPb3zSebrzU_0Yclpwb4Q

Note that youtube-search.py requires a valid API key to be passed to it as argument of the command line option --app-key or, otherwise, passed on as the environment variable YOUTUBE_DATA_APP_KEY. (Use the command line option --help for brief helping info.)

try this

import requests
from bs4 import BeautifulSoup

resp = requests.get('https://www.youtube.com/onepiece')
soup = BeautifulSoup(resp.text, 'html.parser')

channel_id = soup.select_one('meta[property="og:url"]')['content'].strip('/').split('/')[-1]

I recently had a similar requirement where I knew the custom URL of a youtube channel and I want to get the channel id. So I went to the YouTube channel and clicked on a random video to get the Video ID from the web browser url. Once I get the Video Id, then I used the "Videos: list" API to get the video details:

https://youtube.googleapis.com/youtube/v3/videos?part=snippet&id=[VEDIO_ID]&key=[YOUR_API_KEY]

The retuned data contained all the metadata information about the video, including ChannelId :-).

It may not be the most smartest and elegant way of doing things, but it solved my purpose.

Related