Understanding Web Forms
The other day I was asked to help automate getting data from a
Government web page:
(link dead, sorry)
If you go to that page, you can drill into a state, select a station near you, and get precipitation data for certain months. That's fine, but my customer wanted to take the human element out of it: to download the data automatically. Writing a Perl script or even a shell script (with "curl" or lynx) isn't hard, but how can we get the right selections?
It's just a script
If you pay careful attention, you'll notice that after you select a state and click "Continue", the address hasn't changed: you are still at www.ncdc.noaa.gov/servlets/DLYP. That means that DYLP is a script, a script that interprets forms. We can confirm that by looking at the source (View Source) and noting this section:
<FORM METHOD=POST ACTION="DLYP"> <FONT SIZE=+2><B>Select State<BR>Where Desired Station is Located</FONT></FONT><BR> <SELECT NAME="state" SIZE=5> <OPTION VALUE="AL" SELECTED>Alabama <OPTION VALUE="AK">Alaska
That tells me that to get to the next page, I could do it in one step by using a url like this:
Now take a look at that page and you'll see something very important:
<FORM METHOD=POST ACTION="DLYP"> <input type="hidden" name="state" value="+state+"> <FONT SIZE=+2><B>State: MA<BR><P>Select Desired Station</FONT></FONT></FONT><BR><P> <FONT><SELECT NAME="coban" SIZE=5> <OPTION VALUE="190120" SELECTED>AMHERST 190120 <OPTION VALUE="190190">ASHBURNHAM 190190 <OPTION VALUE="190408">BARRE FALLS DAM 190408
input type="hidden" name="state" value="+state+
The variable that we already passed has been encoded into the new page so that it can be passed on. We continue building our string:
and this brings us directly to a page where we can select the month and type of output we desire. If we look at the source, we see that the programmer has changed things. The "state" and "coban" variables are gone, and have been replaced with a new variable.
<form METHOD=POST ACTION="DLYP" TARGET="_TOP"> <font SIZE=+2><b>Select Desired YearMonth for<br><font COLOR="CC3300">Coop ID 190120</font></b><br></font> <select NAME="VARVALUE" SIZE=5> <option VALUE="190120200312AMHERST" SELECTED>200312 (190120, AMHERST) <option VALUE="190120200207AMHERST">200207 (190120, AMHERST) </select> <p> <input type=hidden name="coopid" value="190120"> <p> <b> <font COLOR=CC3300>View</font> <input TYPE=radio NAME="which" VALUE="DLYPFORM" CHECKED>Web Form <input TYPE=radio NAME="which" VALUE="ascii">ASCII <p><input type=submit VALUE="Continue"> </form></b> </form>
So, our final url is going to be:
and we could do something as simple as
lynx -dump -nolist "http://www.ncdc.noaa.gov/servlets/DLYP?coopid=190120&VARVALUE=190120200312AMHERST&which=ascii"
to get it. We'd use shell scripting or Perl or whatever to construct the command with the data we want, and probably another script to parse the results, but that's all easy to do.
The "200312" is for December 2003. If you are trying this at some later time, change that part of the url.
Note that if any values had spaces or other special characters, we'd need to encode them. For example,
So, all we need is to get the "coopid" and station name for the places we are interested in, and construct a date string, and we can get whatever we want (assuming they have it, of course) whenever we want from their web form.
Got something to add? Send me email.
(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version
Increase ad revenue 50-250% with Ezoic