Scraping consulta de tasas de depósito a plazo de bancos en Perú
El ejemplo esta desarrolado en python con django lo cual requiere que tengas instalado:
-Django y request
*************************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 Tasas de depósitos a plazo</div>
<div class="fila">
<input type="text" id="txtIdentificador" value=""/>
<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/Demo41.js' %}" ></script>
</body>
</html>
*************************JS*******************************
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 requests
def index(request):
return render(request,"Demo41.html")
def procesar(request):
codigo=request.POST.get("data")
rptaJson={"data":[]}
print("buscar codigo "+codigo)
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()
req=sesion.get("https://comparabien.com.pe/depositos-plazo",headers=header,verify=False)
if req.status_code==200:
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",
"origin":"https://comparabien.com.pe",
"referer":"https://comparabien.com.pe/depositos-plazo",
"content-type":"application/x-www-form-urlencoded"
}
payload="currency=MN&balance=S%2F+100%2C000&days=360+d%C3%ADas&exclude=on&geo=LI&email=correo%40gmail.com&news=on&source=Compara&prod_type=depositos-plazo"
req=sesion.post("https://comparabien.com.pe/depositos-plazo/result",data=payload,headers=header,verify=False)
print(req.status_code)
if req.status_code==200:
html=req.text
hash=html.find("hash")
posValue=html.find(">",hash)
poscomilla=html.find("<",posValue+1)
csrftoken=html[posValue+1:poscomilla]
print(csrftoken)
csrftoken=csrftoken.replace("$","%24").replace("/","%2F")
url="https://comparabien.com/services/pe/ws-depositos-plazo.php?callback=jQuery3710005224522574193702_1767738482235&sEcho=1&sWhere=&ipaddr=&userid=&username=&geo=LI&balance=100000&days=360¤cy=MN&exclude=on&email=correo%40gmail.com&source=Compara&hash="+csrftoken+"&iSortingCols=1&iSortCol_0=6&sSortDir_0=desc&bSortable_6=true"
req=sesion.get(url,headers=header,verify=False)
if req.status_code==200:
datatxt=req.text
pos=datatxt.find("(")
posfin=datatxt.find(")",pos)
jsonData=json.loads( datatxt[pos+1:posfin])
for i in jsonData["aaData"]:
rptaJson["data"].append( {"banco":i[4],
"tasa":i[6],
"GananciaTotal":i[5],
"GananciaMensual":i[17],
"MontoMinimoApertura":i[10],
"TieneFSD":i[16],
"fechaactualizacion":i[14]})
except Exception as e:
print("Error "+ str(e))
return JsonResponse(rptaJson,safe=False)
Comentarios
Publicar un comentario