"""07. Cotejo (d), red: fichas públicas de url_origen de la muestra. ≤ 30 peticiones, ≥ 3 s, parada ante 403/429. Registro en interno/cotejo/peticiones.log. HTML en interno/cotejo/fichas/caso_NN.html.""" import json, time, datetime as dt, urllib.request, urllib.error, ssl UA = 'Mozilla/5.0 (X11; Linux x86_64) buscalicitaciones-cotejo/1.0 (+https://buscalicitaciones.com)' m = json.load(open('/w/interno/cotejo/muestra_INTERNO.json')) LOG = open('/w/interno/cotejo/peticiones.log', 'a'); n = 0 for d in m: u = d['adjudicacion'][0].get('url_origen') if d['adjudicacion'] else None if not u or u == 'None' or not u.startswith('http'): continue if n >= 30: break if n: time.sleep(3.2) n += 1 try: req = urllib.request.Request(u, headers={'User-Agent': UA, 'Accept-Language': 'es-ES,es;q=0.9'}) r = urllib.request.urlopen(req, timeout=60, context=ssl.create_default_context()); body = r.read(); st = r.status except urllib.error.HTTPError as e: body = e.read() if hasattr(e, 'read') else b''; st = e.code except Exception as e: body = str(e).encode(); st = -1 LOG.write(f"{dt.datetime.utcnow().isoformat()}Z\tcaso {d['caso']}\t{u}\t{st}\t{len(body)}\n"); LOG.flush() print(d['caso'], st, len(body), flush=True) open(f"/w/interno/cotejo/fichas/caso_{d['caso']:02d}.html", 'wb').write(body) if st in (403, 429): print('PARADA por', st); break print('peticiones', n)