Grasping for Tags
August 9, 1999
Presumably, these comments are used by the Weather
Underground's own processing scripts but they benefit us
as well. Our script need only search for the temperature
comment to grab the current reading ... which is exactly
what we do to pluck the temperature from this page, followed
by a conversion to Fahrenheit (the Weather Underground's
internal data assumes Celsius, the metric standard).
Plucking the temperature from a complex page.
#grab temperature and convert to Fahrenheit
if
($weatherPage=~/<!-- Temperature == ([0-9]+\.[0-9]+) -->/i)
{$weatherTemp=$1;
#convert C to F below
$weatherTemp=int(($weatherTemp*1.8)+32);
$weatherTemp.="° F"
} else
{$weatherTemp="N/A"}
|
An if clause determines whether the retrieved page
contains a temperature comment tag at all. If, for instance,
the page was unavailable due to network problems or a site
problem, this if test would fail and the
$weatherTemp variable would receive the value
"N/A", or "Not Available".
Notice the regular expression match used to locate the
temperature comment tag. It tries to match the tag exactly
as it appears in the HTML document yet allows for any possible
temperature in the clause "([0-9]+\.[0-9]+)".
Translated, we can read this
regular expression
as "one or more of any digit followed by a decimal
followed by one or more of any digit". Because this
clause is grouped within parentheses the matching portion
will be automatically assigned to the Perl variable $1
(if there was a second grouped clause its match would be
assigned to $2 and so on).
We assign the temperature -- $1 -- to
$weatherTemp and then perform a few calculations to
convert the number into Fahrenheit and then tack on the HTML
degree symbol and Fahrenheit label. Now that we've got the
temperature, it can easily be substituted into the correct
placeholder, completing the new weather-aware Smallville
Gazette.
View a live demo
of the enhanced Gazette.
smallville2.cgi: New weather-aware version of
the Smallville Gazette.
#!/usr/bin/perl
use CGI;
use LWP::Simple;
$cgiobject=new CGI;
$cgiobject->use_named_parameters;
print $cgiobject->header;
#read in template page
$templateFile="smallville.html";
open(INFILE,"<$templateFile");
@templatePage=<INFILE>;
close(INFILE);
#condense page array into one scalar variable
$resultPage=join("",@templatePage);
#determine current date
($sec,$min,$hour,$mday,
$mon,$year,$wday,$yday,$isdst)=localtime(time);
$curDay=(Sunday,Monday,Tuesday,Wednesday,
Thursday,Friday,Saturday)[$wday];
$curMonth=(January,February,March,April,
May,June,July,August,September,
October,November,December)[$mon];
$liveDate="$curDay, $curMonth $mday, ".($year+1900);
#search-and-replace on date
$resultPage=~s/<!--INSERT DATE HERE-->/$liveDate/g;
#set URL to weather web page
$weatherURL="http://www.wunderground.com/cgi-bin/
findweather/getForecast?query=14850";
#retrieve this web page
$weatherPage=get($weatherURL);
#grab temperature and convert to Fahrenheit
if
($weatherPage=~/<!-- Temperature == ([0-9]+\.[0-9]+) -->/i)
{$weatherTemp=$1;
#convert C to F below
$weatherTemp=int(($weatherTemp*1.8)+32);
$weatherTemp.="° F"
} else
{$weatherTemp="N/A"}
#search-and-replace on weather
$resultPage=~s/<!--INSERT WEATHER HERE-->/$weatherTemp/g;
#done, output page to browser
print $resultPage;
|
Simply, LWP::Simple
The Perl You Need to Know
Pulling Tags Like Taffy: TokeParser
|