book graphic unix and linux troubleshooting guide

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







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


Hate these ads?

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:


ad

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:



Enter your email address for automatic notification of new posts here
(be sure to whitelist 'feedburner.com' if you use spam filtering)

Or use any RSS reader

Delivered by FeedBurner

cartoon
Need eyes on the ground at your customer's site?
Installation and light training Boston and New England
Reliable and experienced, punctual and professional.


Views for this page
Today This Week This Month This Year  Overall
271111 3,845

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

pavatar.jpg
More:
       - Basics
       - Web/HTML




Unix/Linux Consultants


http://thatitguy.com Business networking servers, Linux and Unix experts. In business since 1997! Windows and Exchange to Samba and Scalix migration experts.


http://www.cleverminds.net Need expert advice? Want a second opinion? CleverMinds is a one-stop-shop for a wide range of technology solutions. We support Unix, Linux, SCO as well as CMS, ecom, blogs, podcasts, search engines consulting and more. Contact us at web2.0@cleverminds.net 0r (617) 894-1282


UBB Computer Services Support for Openserver, Unixware and Linux. Windows integration with Unix/Linux servers. Hardware, Backup and Networking issues. Located near Sacramento CA, we provide onsite support throughout Northern CA and Nationwide via remote access. We are a SCO Authorized Partner and a Microlite BackupEdge Certified Reseller.



Twitter
o My wife says her new cellphone takes fuzzy pictures. I peeled off the plastic covering the lens.. 11:03 AM Feb 9th 2008




card_image







Change Congress

Publish your articles, comments, book reviews or opinions here!