youtube / app.py
soiz's picture
Create app.py
3141bb9 verified
import requests
from bs4 import BeautifulSoup
import gradio as gr
def search_google_videos(query):
# 検索クエリをエンコードしてGoogleのURLを作成
url = f'https://www.google.com/search?q={query}&tbm=vid'
# GoogleにGETリクエストを送信
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36'
}
response = requests.get(url, headers=headers)
# レスポンスを解析
soup = BeautifulSoup(response.text, 'html.parser')
# 動画の検索結果を取得
results = []
for result in soup.find_all('div', class_='BVG0Nb'):
title = result.find('h3').text
link = result.find('a')['href']
results.append(f"Title: {title}\nLink: {link}")
return "\n\n".join(results)
# Gradio インターフェースを作成
iface = gr.Interface(
fn=search_google_videos, # 実行する関数
inputs="text", # テキスト入力を使用
outputs="text", # テキスト出力を使用
title="Google Video Search", # タイトル
description="Enter a query to search videos on Google." # 説明
)
# Gradio アプリを起動
iface.launch()