google_search / app.py
mgokg's picture
Update app.py
3201d2f verified
raw
history blame
1.07 kB
import gradio as gr
from googlesearch import search
import requests
from bs4 import BeautifulSoup
def google_search(query):
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36"
}
# Führt die Suche durch und erhält das erste Ergebnis
for result in search(query, num_results=1):
url = result
#return url
break
# Holt den Inhalt der Webseite
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
#first_div = soup.find('div', class_='MjjYud')
first_div = soup.find('body')
if first_div:
return first_div.text.strip()
else:
print("Kein div mit der Klasse 'MjjYud' gefunden.")
return None
demo = gr.Interface(
fn=google_search,
inputs=gr.Textbox(lines=1, placeholder="Geben Sie Ihre Suchanfrage ein..."),
outputs="text",
title="google websearch",
description="Geben Sie eine Suchanfrage ein..."
)
demo.launch()