Automatiza búsquedas de veterinarios con Python

 En este ejemplo mostramos un scraping masivo para consultar estados de un veterinario de lima en python solo utilizando requests.


******************************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 Consulta Veterinario</div>
    <div class="fila">
        <textarea id="txtIdentificador" value="" style="width: 258px; height: 54px;"></textarea>
       <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/Demo74.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
from django.conf import settings
import requests
requests.packages.urllib3.disable_warnings()
def index(request):
    return render(request,"Demo74.html")
def procesar(request):
    cmvps=request.POST.get("data")
    rptaJson=[]
    try:
        acmvp=cmvps.split(",")
        for cmvp in acmvp:
            rptaJson.append(consultaveterinario(cmvp))
    except Exception as e:
        print("Error "+ str(e))
    return JsonResponse(rptaJson,safe=False)
def consultaveterinario(cmvp):
    objVeterinario={}
    try:
        header={"user-agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/146.0.0.0 Safari/537.36",
                "x-requested-with":"XMLHttpRequest",
                "host":"cmvl.pe"}
        sesion=requests.session()
        req=sesion.get("https://cmvl.pe/search/api?q="+cmvp,headers=header,verify=False)
        if req.status_code==200:
            rpta=req.json()
            if "success" in rpta and rpta["success"]:
                for obj in rpta["sugerencias"]:
                    if obj["cmvp"]==int(cmvp):
                        objVeterinario["cmvp"]=cmvp
                        objVeterinario["nombres"]=obj["nombre_completo"]
                        objVeterinario["habilitado"]=obj["habilitado"]
                        objVeterinario["especialidad"]=obj["especialidad"]
                        break
        if len(objVeterinario)==0:
            objVeterinario["cmvp"]=cmvp
            objVeterinario["mensaje"]="No se encontro informacion"
    except Exception as e:
        print("Error "+ str(e))    
    return objVeterinario


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