C贸mo automatizar la consulta de vehiculos por placa en Per煤 con Python 馃殌

  En este ejemplo te muestro c贸mo automatizar la consulta de vehiculos por placa en Per煤 usando Python y Web Scraping.

Vamos a crear un script real utilizando la librer铆a requests para extraer informaci贸n directamente desde la web y procesarla autom谩ticamente.


********************************HTML***********************************
<!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Index</title> {% load static %} <link rel="stylesheet" type="text/css" href="{% static 'styles/general.css' %}" /> <style> .cntpreview{ background: #000; padding: 10px; width: 50%; min-height: 300px; color: #fff } </style> </head> <body> <div id="divLoading" class="wrap_loading hide"> <div class="lds-ring"> <div></div> <div></div> <div></div> <div></div> </div> <div class="loading_text">Procesando ...</div> </div> <div class="titulo">Consulta vehiculo por placa Per煤</div> <div class="fila"> <input type="text" id="txtIdentificador" value="BRN635" /> <button id="btnProcesar" class="boton">Procesar</button> </div> <div style="display: flex;justify-content: center;"> <pre id="txtPre" class="cntpreview"></pre> </div> <div> {% csrf_token %} </div> <script src="{% static 'scripts/Demo55.js' %}" ></script> </body> </html>

********************************JAVASCRIPT***********************************
window.onload=function(){ let btnProcesar=document.getElementById("btnProcesar"); btnProcesar.onclick=function(){ let txtFecha=document.getElementById("txtIdentificador").value; let fd=new FormData(); fd.append("data",txtFecha); servidor({url:"procesar",data:fd,responsetype:"json"}).then((data)=>{ document.getElementById("txtPre").innerHTML=JSON.stringify(data,null,2); }); } } function servidor({ metodo = "post", url = null, data = null, responsetype = "text" } = {}) { return new Promise((resolve, reject) => { let divLoading=document.getElementById("divLoading"); if(divLoading){ divLoading.classList.remove("hide"); } let xhr = new XMLHttpRequest(); xhr.open(metodo, url); var csrftoken=document.getElementsByName("csrfmiddlewaretoken")[0].value; xhr.setRequestHeader("X-CSRFToken", csrftoken); xhr.responseType = responsetype; xhr.onreadystatechange = function () { if (xhr.readyState == 4 && xhr.status == 200) { divLoading.classList.add("hide"); resolve(xhr.response); } } xhr.onerror = function (e) { reject(e) } xhr.send(data); }); }

********************************PYTHON***********************************
from django.shortcuts import render from django.http.response import HttpResponse,JsonResponse import requests import json def index(request): return render(request,"Demo56.html") def procesar(request): placa=request.POST.get("data") rptaJson={} print("buscar placa "+placa) try: header={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36"} sesion=requests.session() payload={"resource":"c2d4a1fe-fc64-43ee-8eb2-e57de4995e21"} req=sesion.post("https://servicewapdigitalprd0100.azurewebsites.net/ecommerce-autos/auth/v2/token/",data=json.dumps(payload),headers=header,verify=False)# if req.status_code==200: rpta=req.json() token=rpta["access_token"] header["host"]="api.pacifico.com.pe" header["aplicacion-id"]="ECAU" header["nombre-aplicacion"]="Ecommerce Autos Clientes" header["ocp-apim-subscription-key"]="60f96b61fef444498478d978c84265f1" header["transaccion-id"]="9e4603ac-5181-4fc8-9afe-10862ed72d9b" header["nombre-servicio-consumidor"]="fe-ecommerceautos" header["authorization"]=token req=sesion.get("https://api.pacifico.com.pe/apigw/ecau/ux-gestion-poliza-autos/v1/vehiculos/"+placa,headers=header,verify=False) if req.status_code==200: rpta=req.json() if "datos" in rpta: datos=rpta["datos"] rptaJson["tipoVehiculo"]=datos["tipoVehiculo"] rptaJson["claseVehiculo"]=datos["claseVehiculo"] rptaJson["numeroAsientos"]=datos["numeroAsientos"] rptaJson["numeroSerieChasis"]=datos["numeroSerieChasis"] rptaJson["areaCirculacion"]=datos["areaCirculacion"] rptaJson["marca"]=datos["marca"]["descripcion"] rptaJson["modelo"]=datos["modelo"]["descripcion"] except Exception as e: print("Error "+ str(e)) return JsonResponse(rptaJson,safe=False)

Comentarios

Entradas populares de este blog

Web Scraping en Argentina: b煤squeda de personas por DNI paso a paso

¡Mira C贸mo Obtengo Datos de un Veh铆culo Solo con la Placa! (Web Scraping)

Bot whatsapp con whatsapp-web.js y nodejs