Web Scraping consulta persona en el REDAM(Registro de Deudores Alimentarios Morosos)
El proyecto esta hecho en python con django.
para replicarlo necesitas crear un proyecto en django con las siguientes librerias
json, requests, easyocr, numpy,Pillow
*********************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">Scrapping Registro de deudores alimentarios morosos (REDAM)</div>
<div class="fila">
<input type="text" id="txtIdentificador" value="23269633,45300024"/>
<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/Demo20.js' %}" ></script>
</body>
</html>
*********************JAVASCRIPT*********************
window.onload=function(){
let btnProcesar=document.getElementById("btnProcesar");
btnProcesar.onclick=function(){
let txtIdentificador=document.getElementById("txtIdentificador").value;
let fd=new FormData();
fd.append("data",txtIdentificador)
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 json
import requests
import easyocr
import io
from PIL import Image
import numpy as np
def index(request):
return render(request,"Demo20.html")
def procesar(request):
dni=request.POST.get("data")
rptaJson=None
print("buscar dni "+dni)
try:
headers={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/132.0.0.0 Safari/537.36",
"sec-ch-ua-platform":"\"Windows\""
}
sesion=requests.Session()
_req=sesion.get("https://casillas.pj.gob.pe/redam",headers=headers)
if _req.status_code==200:
_req=sesion.get("https://casillas.pj.gob.pe/redam/Captcha.jpg")
imgByte=io.BytesIO(_req.content)
img=Image.open(imgByte)
imgnp=np.array(img)
reader = easyocr.Reader(['en'])
result=reader.readtext(imgnp, detail = 0, paragraph=True)
captcha=result[0].replace(" ","").strip()
print(captcha)
urlConsulta="https://casillas.pj.gob.pe/redam/deudoresPorDocumento/?hash_id=0.17132773359189124&captcha="+captcha+"&numerodocumento="+dni+"&tipoDocumento=01++"
_req=sesion.get(urlConsulta,headers=headers)
print(_req.status_code)
if _req.status_code==200:
rptaJson=_req.json()
if(len(rptaJson["data"])>0):
id=rptaJson["data"][0]["id"]
objPersona={"apePaterno":rptaJson["data"][0]["apePaterno"],
"apeMaterno":rptaJson["data"][0]["apeMaterno"],
"nombres":rptaJson["data"][0]["nombres"],
}
urlConsulta="https://casillas.pj.gob.pe/redam/deudasPorDeudor/?hash_id=0.34172542348087465&id="+str(id)
_req=sesion.get(urlConsulta,headers=headers)
if _req.status_code==200:
rptaJson=_req.json()
rptaJson=rptaJson["data"][0]
rptaJson.update(objPersona)
except Exception as e:
print("Error "+ str(e))
##https://casillas.pj.gob.pe/redam/#/
return JsonResponse(rptaJson,safe=False)
Comentarios
Publicar un comentario