File size: 1,296 Bytes
be7cc52 43614ad a8c57d0 be7cc52 e736965 be7cc52 43614ad be7cc52 358f05c be7cc52 43614ad be7cc52 358f05c be7cc52 |
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 |
from fastapi import FastAPI
from playwright.async_api import async_playwright
app = FastAPI()
async def scrape_google(query: str):
url = (
"https://www.google.com/search"
f"?q={query}"
"&sxsrf=AE3TifOZcTbH54cOkE27wqRqSVEmaqb7fw%3A1750003707838"
)
async with async_playwright() as pw:
browser = await pw.chromium.launch(headless=True)
context = await browser.new_context()
page = await context.new_page()
# Accept cookie/consent pop-ups
try:
btn = await page.wait_for_selector('button:has-text("I agree")', timeout=5000)
await btn.click()
except:
pass
await page.goto(url, wait_until="domcontentloaded")
await page.wait_for_selector("h3")
results = []
for h in await page.query_selector_all("h3"):
try:
link = await h.evaluate("(e) => e.closest('a').href")
title = await h.inner_text()
results.append({"title": title, "link": link})
except:
continue
await browser.close()
return results
@app.get("/search")
async def search(query: str):
data = await scrape_google(query.replace(" ", "+"))
return {"query": query, "results": data}
|