Consult DNI with JSOUP

0

Good evening I have this problem when making the query of a DNI from this page, I hope you can help me. This is the page I want to access:

link

This is my code in Java:

  public static Document getHtmlDocument(String url){
      Document doc=null;
      FormElement form=null;
      HTMLDocument pag=null;
    try {
       doc=Jsoup.connect(url).data("txtCongrDNI","75624412").userAgent("Mozilla/56.0.1").timeout(10000).post();   
       //LO QUE QUIERO REALIZAR ES QUE ME MUESTRE LOS DATOS PERSONALES COMO EL NOMBRE Y APELLIDOS
    } catch (IOException e) {
        System.out.println("Excepción al obtener el HTML de la página" + e.getMessage());
    }
    return doc;
}

This only returns the source code of the page:

    
asked by Jcastillo 12.11.2017 в 04:09
source

1 answer

0

You have to pass the hidden elements of the form so that when you do POST you get the correct query.

import java.io.IOException;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;

public class Parser {

    public static void main(String[] args) throws IOException {

        Connection.Response loginForm = Jsoup.connect("http://www.votoinformado.pe/voto/miembro_mesa.aspx")
                .method(Connection.Method.GET)
                .execute();

        Document doc = Jsoup.parse(loginForm.body());
        Elements tag = doc.select("input[name=__VIEWSTATE]");
        String viestate = tag.attr("value");

        tag = doc.select("input[name=__VIEWSTATEGENERATOR]");
        String viewstategerator = tag.attr("value");

        tag = doc.select("input[name=__EVENTVALIDATION]");
        String eventvalidation = tag.attr("value");

        Document finalDoc = Jsoup.connect("http://www.votoinformado.pe/voto/miembro_mesa.aspx")
                .data("txtCongrDNI", "75624412")
                .data("__LASTFOCUS", "")
                .data("__EVENTTARGET", "btnCongrDNI")
                .data("__EVENTARGUMENT", "")
                .data("__VIEWSTATE", viestate)
                .data("__VIEWSTATEGENERATOR", viewstategerator)
                .data("__EVENTVALIDATION", eventvalidation)
                .userAgent("Mozilla")
                .post();


        String DNI =  finalDoc.select("span#lblDNI").text();
        String fullName =  finalDoc.select("span#lblNombres").text();
        System.out.println("The ID number is "+DNI); 
        System.out.println("The full name is "+fullName);
    }
}
    
answered by 12.11.2017 / 06:01
source