Spaces:
Running
Running
import gradio as gr | |
import requests | |
from bs4 import BeautifulSoup | |
import re | |
def search_fn(query): | |
page = requests.get(f"https://www.google.com/search?q={query}") | |
soup = BeautifulSoup(page.content) | |
#links = soup.findAll("a") | |
links = soup.findAll("a") | |
file = open("myfile.txt", "w") | |
for link in soup.find_all("a",href=re.compile("(?<=/url\?q=)(htt.*://.*)")): | |
out = (re.split(":(?=http)",link["href"].replace("/url?q=","").split("&sa",1)[0])) | |
out = out[0] | |
frame_l=f'<div class="container"><iframe class="responsive-iframe" src="{out}" frameborder="0" width="100%" height="2000"></iframe></div>' | |
file.writelines(frame_l) | |
#print(file1.read()) | |
print (out) | |
file.close() | |
with open("myfile.txt", "r") as file1: | |
html_out = file1.read() | |
return (html_out) | |
def first(): | |
out = '''<h1>Loading''' | |
return out | |
def test(inp): | |
with open("myfile.txt", "r") as file1: | |
html_out = file1.read() | |
style = ''' | |
.container { | |
position: relative; | |
overflow: hidden; | |
width: 100%; | |
padding-top: 56.25%; /* 16:9 Aspect Ratio (divide 9 by 16 = 0.5625) */ | |
} | |
/* Then style the iframe to fit in the container div with full height and width */ | |
.responsive-iframe { | |
position: absolute; | |
top: 0; | |
left: 0; | |
bottom: 0; | |
right: 0; | |
width: 100%; | |
height: 100%; | |
} | |
''' | |
out = f'''<!DOCTYPE html> | |
<html lang="en"> | |
<head> | |
</head> | |
<style> | |
{style} | |
</style> | |
<body> | |
{html_out} | |
</body> | |
</html>''' | |
return out | |
with gr.Blocks() as app: | |
with gr.Row(): | |
search_box=gr.Textbox("Search") | |
search_btn=gr.Button() | |
out_text = gr.Textbox() | |
with gr.Row(): | |
input = gr.Textbox() | |
btn = gr.Button() | |
output = gr.HTML("""""") | |
search_btn.click(search_fn,search_box,out_text) | |
btn.click(first,None,output).then(test,input,output) | |
app.launch() |