I want to analyse Chilean elections data available in the official website. What I have returns the data for president (numbers are strings, to be parsed later).

Import["http://www.eleccionservel.cl/ELECCIONES2013/vistaPaisPresidente", "Data"][[2, 2, 2, 2, 2 ;; 10, {1, 3, 4}]]

But to get the data for congress, the website does not have a direct URL and the data by districts needs to be selected by clicking in links that look like this

<a href="#" onclick="javascript:doSubmit('409','D');"> Distrito 9 </a>

Which in turn runs

function doSubmit(a,b){document.consultarForm.codigo.value=a;document.consultarForm.nivel.value=b;document.consultarForm.submit()}
<form id="diputado" name="consultarForm" action="/ELECCIONES2013/diputado" method="POST">

I have tried unsucessfully the following

Import[url, "Data", "RequestMethod" -> "POST", "RequestParameters" -> {   "codigo" -> "409",   "nivel" -> "D",   "division" -> "POLITICA",   "codigoPadre" -> "409",   "codigoCircunscripcion" -> "409",   "codigoColegio" -> "D"   }]

Not sure if "RequestParameters" should refer to the "name" or the "id" of the form.

Is there a chance to get this data with Mathematica in an elegant and simple way, or should I be looking to a different tool?

  • 2
    $\begingroup$ (Nothing personal in flagging this question for closing. The issue appears to have more to do with an opaque Javascript-manipulated HTML form, not to do with Mathematica, in which your syntax was correct and fine.) $\endgroup$ Nov 18 '13 at 12:17
  • $\begingroup$ @acheong87 I guess the question was about finding a way around opaque Javascript-manipulated HTML form, using Mathematica. The solutions uses a non-documented feature and I thought I may have misunderstood it's use. Sorry if the question seems to be out of topic, it is my first post in here and I did try my best to apply due diligence. $\endgroup$
    – rhermans
    Nov 18 '13 at 13:12
  • $\begingroup$ No, in light of that view, your question may have been perfectly relevant—I'm new to Mathematica.SE myself and I don't pretend to have any authoritative view on this. That said, I'm doubtful Mathematica should be able to help in that regard, because then Mathematica would have to be able to not only understand Javascript (a monumental task in itself, but then again, I may be underestimating Mathematica), but also determine what code is relevant. What happens, for example, if the website uses a complex jQuery extension to validate form fields before submitting the form conditionally? $\endgroup$ Nov 18 '13 at 13:36

Your code works fine—the site is just very stringent on the data supplied. I used Chrome's Inspect Element to see the values of all the input elements (including the hidden fields, as you'd noticed)—and I found that sometimes codigoColegio was left blank:

Inspect Element

It didn't work when I filled out codigoColegio to match nivel as you seemed to have done, but it did work when I followed the input fields exactly:


(The copy-pasteable code is already in OP's post, so supplying a screenshot for convenience.)


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.