Pythonでyoutube-search-scraperを使用しYouTubeで検索を行い検索結果を自動的に取得する

スポンサーリンク

Pythonでyoutube-search-scraperを使用しYouTubeで検索を行い検索結果を自動的に取得してみます。

取得のためには、youtube-search-scraperライブラリと、Webブラウザを自動で起動するために、bot_studioライブラリが必要です。なお、youtube-search-scraperライブラリは、Windowsでのみ実行されます。

■Python

今回のPythonのバージョンは、「3.8.5」を使用しています。(Windows10)(pythonランチャーでの確認)

■bot-studioをインストールする

まずは、bot-studioをインストールを行いますが、今回はpipを経由してインストールを行うので、まずWindowsのコマンドプロンプトを起動します。

pip install bot-studio

起動後、上記のコマンドを入力し、Enterキーを押します。

なお、今回は、pythonランチャーを使用しており、Python Version 3.8.5にインストールを行うために、pipを使う場合にはコマンドでの切り替えを行います。

py -3.8 -m pip install bot-studio

切り替えるために、上記のコマンドを入力し、Enterキーを押します。

Defaulting to user installation because normal site-packages is not writeable
Collecting bot-studio
Downloading bot_studio-1.1.4.tar.gz (17.7 MB)
|████████████████████████████████| 17.7 MB 157 kB/s
Requirement already satisfied: requests in c:\users\user_\appdata\roaming\python\python38\site-packages (from bot-studio) (2.25.1)
Collecting tqdm
Downloading tqdm-4.61.1-py2.py3-none-any.whl (75 kB)
|████████████████████████████████| 75 kB 5.5 MB/s
Requirement already satisfied: certifi>=2017.4.17 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->bot-studio) (2021.5.30)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->bot-studio) (1.26.5)
Requirement already satisfied: idna<3,>=2.5 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->bot-studio) (2.10)
Requirement already satisfied: chardet<5,>=3.0.2 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->bot-studio) (4.0.0)
Using legacy setup.py install for bot-studio, since package 'wheel' is not installed.
Installing collected packages: tqdm, bot-studio
Running setup.py install for bot-studio ... done
Successfully installed bot-studio-1.1.4 tqdm-4.61.1

Enterキーを押すと、インストールが開始され、上記のように「Successfully installed」と表示されます。これが表示されれば、bot-studioが正常にインストールされたことになります。

■youtube-search-scraperをインストールする

youtube-search-scraperをインストールを行いますが、今回はpipを経由してインストールを行うので、まずWindowsのコマンドプロンプトを起動します。

pip install youtube-search-scraper

起動後、上記のコマンドを入力し、Enterキーを押します。

なお、今回は、pythonランチャーを使用しており、Python Version 3.8.5にインストールを行うために、pipを使う場合にはコマンドでの切り替えを行います。

py -3.8 -m pip install youtube-search-scraper

切り替えるために、上記のコマンドを入力し、Enterキーを押します。

Defaulting to user installation because normal site-packages is not writeable
Collecting youtube-search-scraper
Downloading youtube-search-scraper-1.0.0.tar.gz (2.4 kB)
Requirement already satisfied: requests in c:\users\user_\appdata\roaming\python\python38\site-packages (from youtube-search-scraper) (2.25.1)
Requirement already satisfied: bot-studio in c:\users\user_\appdata\roaming\python\python38\site-packages (from youtube-search-scraper) (1.1.4)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->youtube-search-scraper) (2021.5.30)
Requirement already satisfied: idna<3,>=2.5 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->youtube-search-scraper) (2.10)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->youtube-search-scraper) (1.26.5)
Requirement already satisfied: chardet<5,>=3.0.2 in c:\users\user_\appdata\roaming\python\python38\site-packages (from requests->youtube-search-scraper) (4.0.0)
Requirement already satisfied: tqdm in c:\users\user_\appdata\roaming\python\python38\site-packages (from bot-studio->youtube-search-scraper) (4.61.1)
Using legacy setup.py install for youtube-search-scraper, since package 'wheel' is not installed.
Installing collected packages: youtube-search-scraper
Running setup.py install for youtube-search-scraper ... done
Successfully installed youtube-search-scraper-1.0.0

Enterキーを押すと、インストールが開始され、上記のように「Successfully installed」と表示されます。これが表示されれば、youtube-search-scraperが正常にインストールされたことになります。

■YouTubeで検索を行い検索結果を自動的に取得する

インストール後、YouTubeで検索を行い検索結果を自動的に取得するスクリプトを書いていきます。

■コード

from youtube_search_scraper import *
youtube.search(keyword='大阪')
response=youtube.search_results()
data=response['body']

for item in data:
    print(item['title'])
    print(item['link'])

今回は、YouTubeで「大阪」というキーワードで検索を行いますので、youtube.search()関数の括弧内に「keyword=’大阪’」と指定し、キーワードを渡します。渡されたキーワードで検索が行われ、結果結果の情報がresponse変数に格納されます。

格納後、結果結果の情報の「body」のみを、data変数に格納。その後、for文を使用し、data変数から「タイトル(title)」と「リンク(link)」の情報を全てprint()関数で出力してみます。

■実行

このスクリプトを「youtubesearchscraper.py」という名前で保存し、コマンドプロンプトから実行してみます。

実行してみると、「Windows セキュリティの重大な警告」というウインドウが表示されます。ウインドウ内には「このアプリの機能のいくつかがWindowsファイアウォールでブロックされてい​ます」と表示されています。

名前が「datakund.exe」となっており、このexeファイルのいくつかがブロックされていますので、今回はネットワーク上の通信を「パブリックネットワーク」に選択し「アクセスを許可する」ボタンをクリックします。

クリックすると、Webブラウザ(今回GoogleChrome)が自動的に起動します。起動後、Youtubeにアクセスを開始し、Youtubeの検索バーで自動的にキーワードが入力され、検索結果が表示されます。

表示後、コマンドプロンプトの方では、検索結果の情報が取得され出力されます。今回指定した検索結果のタイトルとリンクも出力されたことが確認できました。

コメント

タイトルとURLをコピーしました