Help us understand the problem. What is going on with this article?

自作Python - Youtube検索プログラム

More than 3 years have passed since last update.

requestsとBeautifulsoup4だけを使ったシンプルな検索です。

使いかた

Y = Youtube([query], result=[num])

queryは検索単語です。日本語対応。
numは検索結果数です。ページネーションの関係で現時点で最大20。ここら辺は各自でいじってくだしあ。

Y.url    >> 検索結果のurlリスト
Y.title  >> タイトルリスト
Y.id     >> IDリスト(ID?っていうの?JaDJbrwi0gkみたいな固有番号)
Y.embed  >> 貼り付け用urlリスト
Y.time   >> 再生時間リスト(需要ないかも)

例)
print(Youtube("アンジャッシュ",result=5).url)

>> ['https://www.youtube.com/watch?v=JaDJbrwi0gk', 'https://www.youtube.com/watch?v=yGPAyqxrado', 'https://www.youtube.com/watch?v=tGl2Dp1v0gw', 'https://www.youtube.com/watch?v=myp35PyYk1A', 'https://www.youtube.com/watch?v=Tt1rkp3h1pM', 'https://www.youtube.com/watch?v=XIJqsqOCorw']
Y.select() >> 検索結果から1つ選択し、上記の情報について辞書を返します。
key = "url","title","id","embed","time"

例)
movie = Y.select()
print(movie["url"]) >> https://www.youtube.com/watch?v=JaDJbrwi0gk

他のダウンロードAPIとかと組み合わせると効率いいかも。

スクリプト

import requests
from bs4 import BeautifulSoup

class Youtube():
    def __init__(self,query,result=10): # max20まで
        search_url = "https://www.youtube.com/results?search_query=" + query
        req = requests.get(search_url)
        soup = BeautifulSoup(req.text.encode(req.encoding).decode('utf-8','strict'),"html5lib")
        h3s = soup.find_all("h3", {"class":"yt-lockup-title"})[0:result+1]

        self.data = [h3 for h3 in h3s]
        self.url = ["https://www.youtube.com" + h3.a.get('href') for h3 in h3s]
        self.title = [h3.a.get("title") for h3 in h3s]
        self.id = [h3.a.get("href").split("=")[-1] for h3 in h3s]
        self.embed = ["https://www.youtube.com/embed/" + h3.a.get("href").split("=")[-1] for h3 in h3s]
        self.time = [h3.span.text.replace(" - 長さ: ","").replace("。","") for h3 in h3s]
        self.info = [h3.text for h3 in h3s] # >>タイトル - 長さ:00:00。

    def select(self):
        values = {"url":self.url,"title":self.title,"id":self.id,"embed":self.embed,"time":self.time}
        info = self.info
        for i in range(len(info)):
            print("%s:%s" % (i,info[i]))
        while True:
            try:
                num = int(input("番号:"))
                break
            except:
                print("番号を正しく入力してください。")
        results = {
            "url":values["url"][num],
            "title":values["title"][num],
            "id":values["id"][num],
            "embed":values["embed"][num],
            "time":values["time"][num],
            }
        return results

if __name__ == '__main__':
    Y = Youtube(input("検索ワード:"),result=5)
    movie = Y.select()
    print(movie["url"])

hitoribucho
ひとり部長 Python3を始める
https://hitoribucho.com
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away