Explorer
/proc/106/root/tmp/meta_ads_probe.py
← Zurück ↓ Download
import asyncio,json,urllib.parse
from playwright.async_api import async_playwright
queries=['Murprotec','HUMICONTROL','Humexpert','ISOTEC Mallorca','Conforthome','First Mallorca','Porta Mallorquina','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 queries:
   u='https://www.facebook.com/ads/library/?active_status=active&ad_type=all&country=ES&q='+urllib.parse.quote(q)+'&search_type=keyword_unordered'
   try:
    r=await pg.goto(u,wait_until='domcontentloaded',timeout=60000); await pg.wait_for_timeout(5000); text=await pg.locator('body').inner_text();
    out.append({'query':q,'http':r.status if r else None,'url':pg.url,'chars':len(text),'has_no_ads':'No ads match your search criteria' in text,'library_ids':__import__('re').findall(r'(?:Library ID|Identificación de la biblioteca):\\s*([0-9]+)',text)[:10],'active_markers':text.count('Active')+text.count('Activo'),'snippet':text[:500]})
   except Exception as e: out.append({'query':q,'error':str(e)})
  await b.close(); print(json.dumps(out,ensure_ascii=False,indent=2))
asyncio.run(main())