import asyncio,json,re,urllib.parse
from playwright.async_api import async_playwright
qs=['Murprotec','First Mallorca','Property Care Mallorca']
async def main():
async with async_playwright() as p:
b=await p.chromium.launch(headless=True,executable_path='/snap/bin/chromium',args=['--no-sandbox']); pg=await b.new_page(); out=[]
for q in qs:
u='https://www.facebook.com/ads/library/?active_status=active&ad_type=all&country=ES&q='+urllib.parse.quote(q)+'&search_type=keyword_unordered'
r=await pg.goto(u,wait_until='domcontentloaded',timeout=60000); await pg.wait_for_timeout(5000); text=await pg.locator('body').inner_text()
ids=re.findall(r'(?:Library ID|Identificación de la biblioteca):\s*([0-9]+)',text)
starts=re.findall(r'Started running on ([^\n]+)',text)
out.append({'query':q,'http':r.status if r else None,'ids':ids[:20],'starts':starts[:20],'text':text[:12000]})
await b.close(); print(json.dumps(out,ensure_ascii=False,indent=2))
asyncio.run(main())