0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

YouTubeの動画再生数をスクレイピングする

Posted at

jseval」を使用してYouTubeの動画再生数データを取得、スクレイピングするプログラムを書いてみます。

dockerをインストールして、Linux環境下で以下のように実行。

docker run --rm jseval \
        --url "https://www.youtube.com/watch?v=kJQP7kiw5Fk" \
        --evaluate "$(cat <<- EOT
            (async function(){

                function waitForElm(selector) { // wait for element
                    return new Promise(resolve => {
                        if (document.querySelector(selector)) {
                            return resolve(document.querySelector(selector));
                        }

                        const observer = new MutationObserver(mutations => {
                            if (document.querySelector(selector)) {
                                resolve(document.querySelector(selector));
                                observer.disconnect();
                            }
                        });

                        observer.observe(document.body, {
                            childList: true,
                            subtree: true
                        });
                    });
                }

                await waitForElm('#count > ytd-video-view-count-renderer > span.view-count.style-scope.ytd-video-view-count-renderer');

                return JSON.stringify(
                    document.querySelector('#count > ytd-video-view-count-renderer > span.view-count.style-scope.ytd-video-view-count-renderer').innerText
                )

            })();
EOT
        )" \
        --headless \
        --output 

なお、この記事は教育目的であり、記事内のプログラムがそれ以外の目的で使用されることを想定していません。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?