Get ChannelID from Youtube Custom URL
Asked Answered
P

4

12

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

Possession answered 25/9, 2017 at 13:50 Comment(5)
please provide full examples of the URLsLeucomaine
See answers and comments in this possible duplicate of How can I get a channel ID from YouTube?Chassepot
I'm already using that request when i have the channelID or username. It's different from this case, since this is a custom URLPossession
I think this answer outlines the only solution I've seen.Koontz
For those who come across this after the latest Data API revision (31st January, 2024), they provided the 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-workingMethylal
E
3

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.

Exciseman answered 20/1, 2021 at 16:58 Comment(0)
A
3

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.)

Avram answered 20/1, 2021 at 17:49 Comment(1)
your first example doesn't work: "error: custom URL "onepiece": no associated channel found". But 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
C
3

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]
Calcifuge answered 29/11, 2021 at 12:30 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ejaculation
L
0

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"
Legit answered 14/4, 2023 at 22:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.