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:
I want to get its channelID, so that I have the link like:
www.youtube.com/user/OnePieceUK
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:
I want to get its channelID, so that I have the link like:
www.youtube.com/user/OnePieceUK
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.
Elaborating on this answer of mine, I'll note here the following facts:
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.)
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.)
python3 youtube-search.py --custom-url @onepiece
works. Note the @
. I'm not sure if this is an error in the provided usage example, or an error in the python script itself, so I'm just reporting this issue to you in a comment. –
Hemispheroid 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]
try my bash script:
#!/bin/bash
# Enter the YouTube channel name
echo "Enter the YouTube channel name or url:"
read channel_name
# Check if the channel name contains "youtube.com" and add it if necessary
if [[ ! "$channel_name" == *youtube.com* ]]; then
channel_name="https://www.youtube.com/$channel_name"
fi
# Use YouTube get the channel ID
channel_id=$(curl -s -A "Mozilla/5.0 (Windows NT 6.1; rv:60.0) Gecko/20100101 Firefox/60.0" --compressed $channel_name | grep -oP '(?<=externalId":")[^"]*(?=",)')
# Print the channel ID and original channel name
echo "The channel ID for ${channel_name##*/} is: $channel_id"
© 2022 - 2024 — McMap. All rights reserved.
forHandle
query parameter to get data of a handle/username. You can refer to this answer for details here: https://mcmap.net/q/102620/-youtube-listchannels-with-username-forusername-is-not-working – Methylal