问题
This is a YouTube channel URL that includes Cyrillic characters in the username:
https://www.youtube.com/c/%D0%9B%D1%83%D1%87%D1%88%D0%B8%D0%B5%D0%B4%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B5%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B/videos
I am trying to obtain the channel's id from the URL by calling the YouTube DATA API v3:
https://www.googleapis.com/youtube/v3/channels?key=[YouTubeAPIkey]&forUsername=%D0%9B%D1%83%D1%87%D1%88%D0%B8%D0%B5%D0%B4%D0%BE%D0%BA%D1%83%D0%BC%D0%B5%D0%BD%D1%82%D0%B0%D0%BB%D1%8C%D0%BD%D1%8B%D0%B5%D1%84%D0%B8%D0%BB%D1%8C%D0%BC%D1%8B&part=id
But the call returns no data.
For reference, "https://www.youtube.com/c/besogontv/videos" returns a valid result:
https://www.googleapis.com/youtube/v3/channels?key=[YouTubeAPIkey]&forUsername=besogontv
Just to see if it may work, I tried decoding the URL encoding and then re-encoding to UTF8, but it didn't make a difference.
Is there some character encoding issue I'm missing?
回答1:
If you'll issue the following command (at any GNU/Linux bash
prompt):
$ wget \
--quiet \
--output-document=- \
--content-on-error \
"https://www.googleapis.com/youtube/v3/channels?key=$APP_KEY&id=UCk8LWzqGcHz21FWysiXuCHw&part=brandingSettings,contentDetails,id,snippet,statistics,status,topicDetails&maxResults=1"
you'll see that лучшиедокументальныефильмы
is not the channel's user name, but its customUrl!
The forUsername property does not function for a given channel's custom URL since these URLs are not guaranteed to uniquely represent any given channel.
Do convince yourself by querying on Google's issue tracker for either of these two phrases channels forusername
or vanity URL
to see the terse/raw official responses users got from Google's staff.
Indeed, at times, the official docs and staff responses do lack useful/meaningful clear-cut specifications and/or formulations. (I already experienced all these myself too!)
As a final note, you may scrap out of the HTML page obtained from https://www.youtube.com/c/лучшиедокументальныефильмы
the channel ID of your interest, but please bear in mind that this activity is forbidden by Google, as per its DTOS docs:
Scraping
You and your API Clients must not, and must not encourage, enable, or require others to, directly or indirectly, scrape YouTube Applications or Google Applications, or obtain scraped YouTube data or content. Public search engines may scrape data only in accordance with YouTube's robots.txt file or with YouTube's prior written permission.
Instead of scraping, I'd recommend using the Search.list API endpoint, invoked with the q
parameter being лучшиедокументальныефильмы
and the type
parameter being channel
(if you're able to cope with the fuzziness implied).
Update upon answering to a related SO question
Here is a simple Python3 script implementing the functionality that you're looking for. Applying your custom URL to this script produces the expected result:
$ python3 youtube-search.py \
--custom-url Лучшиедокументальныефильмы \
--app-key ...
UCk8LWzqGcHz21FWysiXuCHw
$ python3 youtube-search.py \
--user-name Лучшиедокументальныефильмы \
--app-key ...
youtube-search.py: error: user name "Лучшиедокументальныефильмы": no associated channel found
Note that you have to pass to this script your application key as argument to the command line option --app-key
(use --help
for brief help info).
来源:https://stackoverflow.com/questions/62576403/how-to-get-a-youtube-channel-id-from-the-channels-username-which-includes-cyril