File size: 1,722 Bytes
82373ce
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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()