(OLDER) <- More Stuff -> (NEWER) (NEWEST)
Printer Friendly Version



Understanding Web Forms


More Articles

The other day I was asked to help automate getting data from a Government web page: http://www.ncdc.noaa.gov/servlets/DLYP



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 http://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:

http://www.ncdc.noaa.gov/servlets/DLYP?state=MA

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
 

Notice that

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:

http://www.ncdc.noaa.gov/servlets/DLYP?state=MA&coban=190120

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:

http://www.ncdc.noaa.gov/servlets/DLYP?coopid=190120&VARVALUE=190120200312AMHERST&which=ascii

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,

http://www.ncdc.noaa.gov/servlets/DLYP?coopid=301185&VARVALUE=301185200312CANTON%204%20SE&which=ascii

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.


Technorati tags:


Click here to add your comments



Don't miss responses! Subscribe to Comments by RSS or by Email

Click here to add your comments


If you want a picture to show with your comment, go get a Gravatar


ad


Have you tried Searching this site?

Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates

This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more. We appreciate comments and article submissions.

Publishing your articles here

Jump to Comments



Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.

Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.

We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.


book graphic unix and linux troubleshooting guide

My Troubleshooting E-Book will show you how to solve tough problems on Linux and Unix systems!



 I sell and support
 Kerio Mail server




pavatar.jpg
More:
       - Basics
       - Web/HTML


Unix/Linux Consultants

Skills Tests

Guest Post Here











My Favorites

Change Congress