由于频道同名,YouTube API 订阅者计数不正确
YouTube API subscriber count incorrect because of channels with the same name
代码:
import requests
import json
key = 'key' #api key
url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&key='+ key +'&forUsername='
youtuber = input('What\'s the name of the youtuber: ') #get youtuber name
url += youtuber.lower() #youtuber name all lowercase
r = requests.get(url) #get data from youtuber channel webpage
try:
subs = json.loads(r.text)['items'][0]['statistics']['subscriberCount']
except:
print('Your youtuber doesn\'t exist ):')
exit()
print(youtuber,"has",subs,"subscribers! Woohoo!")
没关系:
输入:Google
输出:Google 有 9640000 个订阅者!哇哦!
这是不是:
输入:Markiplier
输出:Markiplier 有 84900 个订阅者!哇哦! (实际上,他的主频道有2780万订阅者)
我运行进入同名频道的不幸问题。我该如何防止这种情况?
您必须承认 YouTube 网站的以下特性:它允许(通过设计)完全相同的名称 - 在您的情况下 Markiplier
- 成为频道的自定义 URL 和,同时,成为另一个频道的(旧)用户名。
请阅读 first few paragraphs of my answer 以了解一个非常相关的问题。从那里,您将很好地了解这两个(有时令人困惑)概念之间的区别:频道用户名和频道自定义URL.
具体来说,如果您使用我的 Python3 public 脚本 youtube-search.py
,您会发现有两个不同的通道表现出我刚才描述的行为 w.r.t。名字 Markiplier
:
$ python3 youtube-search.py --user-name Markiplier
UCxubOASK0482qC5psq89MsQ
$ python3 youtube-search.py --custom-url Markiplier
UC7_YxT-KID8kRbqZo7MyscQ
请注意,youtube-search.py
需要将有效的 API 密钥作为命令行选项的参数传递给它 --app-key
,否则,作为环境变量传递 YOUTUBE_DATA_APP_KEY
。 (使用命令行选项 --help
获取简短的帮助信息。)
进一步 Channels.list
API 端点查询 URL:
https://www.googleapis.com/youtube/v3/channels?id=UCxubOASK0482qC5psq89MsQ,UC7_YxT-KID8kRbqZo7MyscQ&part=id,snippet,statistics&fields=items(id,snippet(title,description,customUrl),statistics(subscriberCount))&maxResults=2&key=...
将确认您所经历的差异:
{
"items": [
{
"id": "UCxubOASK0482qC5psq89MsQ",
"snippet": {
"title": "markiplier",
"description": "I will no longer be updating this channel! All my new videos with be uploaded to markiplierGAME! Please re-subscribe on that channel to stay up-to-date on my videos!"
},
"statistics": {
"subscriberCount": "85000"
}
},
{
"id": "UC7_YxT-KID8kRbqZo7MyscQ",
"snippet": {
"title": "Markiplier",
"description": "Welcome to Markiplier! Here you'll find some hilarious gaming videos, original comedy sketches, animated parodies, and other bits of entertainment! If this sounds like your kind of channel then please Subscribe Today!\n\nTotal Charity Raised ▶ ,000,000+",
"customUrl": "markiplier"
},
"statistics": {
"subscriberCount": "27800000"
}
}
]
}
现在总结一下我的观点w.r.t。您的问题 -- 我 运行 进入同名频道的不幸问题。我如何防止这种情况? --,我注意到以下几点:
- 确定您希望从 API 获得什么:自定义 URLs 和 用户名 是不同的 API 个概念。
- 如果您尝试获取有关您拥有 用户名 的频道的信息,请使用
Channel.list
通过适当的 forUsername
查询的端点]参数。
- 如果您尝试获取有关给定 自定义频道 URL 的信息,请使用我在我的文章中描述的过程和 Python 代码回答这个问题 Obtaining a channel id from a youtube.com/c/xxxx link。还要为该过程可能失败做好准备(由于 API 当前实施的方式,很可能该过程在某些有效的自定义 URL 上失败)。
代码:
import requests
import json
key = 'key' #api key
url = 'https://www.googleapis.com/youtube/v3/channels?part=statistics&key='+ key +'&forUsername='
youtuber = input('What\'s the name of the youtuber: ') #get youtuber name
url += youtuber.lower() #youtuber name all lowercase
r = requests.get(url) #get data from youtuber channel webpage
try:
subs = json.loads(r.text)['items'][0]['statistics']['subscriberCount']
except:
print('Your youtuber doesn\'t exist ):')
exit()
print(youtuber,"has",subs,"subscribers! Woohoo!")
没关系:
输入:Google
输出:Google 有 9640000 个订阅者!哇哦!
这是不是:
输入:Markiplier
输出:Markiplier 有 84900 个订阅者!哇哦! (实际上,他的主频道有2780万订阅者)
我运行进入同名频道的不幸问题。我该如何防止这种情况?
您必须承认 YouTube 网站的以下特性:它允许(通过设计)完全相同的名称 - 在您的情况下 Markiplier
- 成为频道的自定义 URL 和,同时,成为另一个频道的(旧)用户名。
请阅读 first few paragraphs of my answer 以了解一个非常相关的问题。从那里,您将很好地了解这两个(有时令人困惑)概念之间的区别:频道用户名和频道自定义URL.
具体来说,如果您使用我的 Python3 public 脚本 youtube-search.py
,您会发现有两个不同的通道表现出我刚才描述的行为 w.r.t。名字 Markiplier
:
$ python3 youtube-search.py --user-name Markiplier
UCxubOASK0482qC5psq89MsQ
$ python3 youtube-search.py --custom-url Markiplier
UC7_YxT-KID8kRbqZo7MyscQ
请注意,youtube-search.py
需要将有效的 API 密钥作为命令行选项的参数传递给它 --app-key
,否则,作为环境变量传递 YOUTUBE_DATA_APP_KEY
。 (使用命令行选项 --help
获取简短的帮助信息。)
进一步 Channels.list
API 端点查询 URL:
https://www.googleapis.com/youtube/v3/channels?id=UCxubOASK0482qC5psq89MsQ,UC7_YxT-KID8kRbqZo7MyscQ&part=id,snippet,statistics&fields=items(id,snippet(title,description,customUrl),statistics(subscriberCount))&maxResults=2&key=...
将确认您所经历的差异:
{
"items": [
{
"id": "UCxubOASK0482qC5psq89MsQ",
"snippet": {
"title": "markiplier",
"description": "I will no longer be updating this channel! All my new videos with be uploaded to markiplierGAME! Please re-subscribe on that channel to stay up-to-date on my videos!"
},
"statistics": {
"subscriberCount": "85000"
}
},
{
"id": "UC7_YxT-KID8kRbqZo7MyscQ",
"snippet": {
"title": "Markiplier",
"description": "Welcome to Markiplier! Here you'll find some hilarious gaming videos, original comedy sketches, animated parodies, and other bits of entertainment! If this sounds like your kind of channel then please Subscribe Today!\n\nTotal Charity Raised ▶ ,000,000+",
"customUrl": "markiplier"
},
"statistics": {
"subscriberCount": "27800000"
}
}
]
}
现在总结一下我的观点w.r.t。您的问题 -- 我 运行 进入同名频道的不幸问题。我如何防止这种情况? --,我注意到以下几点:
- 确定您希望从 API 获得什么:自定义 URLs 和 用户名 是不同的 API 个概念。
- 如果您尝试获取有关您拥有 用户名 的频道的信息,请使用
Channel.list
通过适当的forUsername
查询的端点]参数。 - 如果您尝试获取有关给定 自定义频道 URL 的信息,请使用我在我的文章中描述的过程和 Python 代码回答这个问题 Obtaining a channel id from a youtube.com/c/xxxx link。还要为该过程可能失败做好准备(由于 API 当前实施的方式,很可能该过程在某些有效的自定义 URL 上失败)。