Gradiobot / app.py
Rgiz's picture
Update app.py
82373ce verified
import asyncio
from playwright.async_api import async_playwright
from bs4 import BeautifulSoup
import gradio as gr
async def buscar_google_shopping(produto):
async with async_playwright() as p:
browser = await p.chromium.launch(headless=True)
page = await browser.new_page()
await page.goto("https://www.google.com/shopping")
await page.fill("input[name='q']", produto)
await page.keyboard.press("Enter")
await page.wait_for_load_state("networkidle")
try:
await page.click("text=Ordenar por")
await page.click("text=Preço mais baixo")
await page.wait_for_load_state("networkidle")
except:
pass
html = await page.content()
await browser.close()
soup = BeautifulSoup(html, "html.parser")
produtos = []
for item in soup.select("div.sh-dgr__content")[:30]:
nome_tag = item.select_one("h3")
preco_tag = item.select_one(".a8Pemb")
link_tag = item.select_one("a")
if nome_tag and preco_tag and link_tag:
nome = nome_tag.text.strip()
preco = preco_tag.text.strip()
link = "https://www.google.com" + link_tag["href"]
produtos.append({"Nome": nome, "Preço": preco, "Link": link})
return produtos
interface = gr.Interface(
fn=buscar_google_shopping,
inputs=gr.Textbox(label="Produto para buscar no Google Shopping"),
outputs=gr.Dataframe(headers=["Nome", "Preço", "Link"]),
title="Google Shopping Scraper",
description="Busca os 30 produtos mais baratos com nome, preço e link."
)
if __name__ == "__main__":
interface.launch()