# Need help with Javascript/ HTML page for data download



## douglatins (Aug 8, 2011)

I have a page that i select some information regarding product import export.
I select from dropdown menus the port which the goods go out, and select in another dropdown menus my detailing, which i select product number and city in another.

So i get import data from a choosen port of each product and from which city they came from.

Then i press query, but its a javascript button, so i lose all the information i sent in the previous page, then i choose to download it, and tick a box for excel file and press send.
But i want to in some way automate that proccess, that by just creating a link i get to download the file, so i can run a script that follows hundred of links and download me the files.

How can i do that?


----------



## streetfighter 2 (Aug 8, 2011)

Ajax?


----------



## douglatins (Aug 8, 2011)

I have a image and a code example of the page

About Ajax i have no idea


----------



## streetfighter 2 (Aug 8, 2011)

Oh, my bad.  I thought you were writing the code for the page.  I see you just want to automate/wrap the interface.

Let me put my thinking cap on . . .  

EDIT:  After looking at the code, at least the part that you provided, I have to say it may be kinda tough.  The form is awfully dynamic (not to mention the code is mostly in Spanish/Portuguese) and it uses AJAX and JSON as far as I can tell.  I'm guessing you'll need to do fill out the proper JSON structure with the data you have then use the page's javascript methods to get a URL to the excel download.  Unfortunately I can't investigate it further without having access to the page in question.

EDIT2:  I think IlluminAce is correct.  The page does use AJAX but the form is still using POST.  For whatever reason my browser wasn't opening the entire code you posted .


----------



## IlluminAce (Aug 8, 2011)

Before I go further, I'd just say if you're intending to make many repeated automated requests to this website, bear in mind that not all webmasters will approve of this - you may be contravening guidelines somewhere. Check first.

That said...


Follow through the javascript to understand what it's doing.


```
<button id="btnPesquisar" class="botao ui-corner-all" type="button">Query</button>
```

Now search for btnPresquisar


```
$('#btnPesquisar').click(function(){
```

... lots of validation JS...


```
if(!formValidate()){

				return false;

			} else {

				$('#frmConsulta').submit();

			}
```

... so what it finally gets round to doing is submitting the form frmConsulta. Which is:


```
<form name="frmConsulta" id="frmConsulta"

	action="http://aliceweb2.mdic.gov.br//consulta-ncm/consultar" method="post"
```

So it simply navigates to the page http://aliceweb2.mdic.gov.br//consulta-ncm/consultar, passing the values you've entered as part of a POST request. To do this programatically, you'd construct a HTTP POST request - get an idea how here, including your values as required (get the correct key names from the code).

Retrieve the page returned, and you'll have to do something similar again to acquire the xls file.


----------



## douglatins (Aug 9, 2011)

IlluminAce said:


> Before I go further, I'd just say if you're intending to make many repeated automated requests to this website, bear in mind that not all webmasters will approve of this - you may be contravening guidelines somewhere. Check first.
> 
> That said...
> 
> ...



It still is very difficult i have no idea what i'm doing


----------



## IlluminAce (Aug 11, 2011)

douglatins said:


> It still is very difficult i have no idea what i'm doing



Hmm, I'd suggest that this may be more work than you're hoping to perform then. You need to understand HTML and JS (which aren't difficult languages to muster, but you still need some understanding), plus another language to code your program (such as Java, Python, etc). I would write a little for you to help get you started, but I honestly don't have the time. 

Failing the programmatic approach, you could automate the process with macro-style applications which record mouse movements and keystrokes. It's highly inelegant, not at all robust, would be very slow to execute, likely to fall over all the time, and probably a real pain to get going in the first place (there's a sales pitch for you!) - you may even need to write code for it, to type in your different inputs - but it _might_ be sort-of simple to throw together. Googling "mouse macro freeware" throws up some results, but I wouldn't really advise this approach.


----------

