How can I retrieve subscriber count from YouTube API without oAuth?

情到浓时终转凉″ 提交于 2020-01-25 11:26:22

问题


I want to retrieve the sub count of a YouTube channel I do not own. I want to use that data object to have a bot tweet out the number every hour. How can I do this without the use of Auth and without using a web browser. This project doesn't use a web browser so I don't want to have to make a html page and have it displayed on a website or something. I just want to get the object and pass it to my twitter bot who will then have it tweeted. I'm using JavaScript and node


回答1:


Update (don't try this approach):

YouTube seems to rely on JS these days to render the page and cheerio will only fetch the static HTML page and parse it. The subscriber account is probably retrieved once the page is rendered and then displayed on the page with JS, which cheerio cannot do. Therefore, the following approach does not work on this specific website.

Original:

Fetch the page with an HTTP request and then parse it with cheerio. Cheerio works a lot like jQuery if you have experience with that - it just parses the HTML and allows you to use jQuery selectors to get the bit of HTML you want.

I also used request to make the HTTP request for me.

Below is a rough example of what you might need to do.

var cheerio = require('cheerio');
var request = require('request');

// Replace "user_account" with the account name.
var url = "https://www.youtube.com/user/user_account";

request({
    method: 'GET',
    url: url
}, function (err, response, html) {
    if (err) {
        console.log(err);

        return;
    }

    let $ = cheerio.load(html);

    var subscribers = $('#subscriber-count').text();

    // use subscribers...
});

Issues

YouTube could change the page at any time which might cause the code not to work as it requires the page to be setup in a specific way.




回答2:


YouTube provides an API which can be queried by going to the following URL:

https://www.googleapis.com/youtube/v3/channels?part=statistics&id={{ID}}&key={{KEY}}

Make sure to replace the {{ID}} with the ID of the channel you want to query and {{KEY}} with your API key. Make sure to install the request node module. With Node.js installed you can just do npm install --save-dev request

var request = require('request');

// Replace id and key with the accountId and an API key.
var id = "UC-lHJZR3Gqxm24_Vd_AJ5Yw";
var key = "AIzaSyAyWsBUsAChiYPDOptU-NBAh_p7oExR1oc";

var url = "https://www.googleapis.com/youtube/v3/channels?part=statistics&id=" + id + "&key=" + key;

request({
    method: 'GET',
    url: url
}, function (err, response, text) {
    if (err) {

        return;
    }

    var json = JSON.parse(text);

    console.log(json.items[0].statistics.subscriberCount);
});

I've tested the code and it's working fine.

Issues

The key will probably eventually stop working as YouTube won't consider it valid any more, causing the request to be invalid.



来源:https://stackoverflow.com/questions/53621309/how-can-i-retrieve-subscriber-count-from-youtube-api-without-oauth

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!