hodge-podge has asked for the wisdom of the Perl Monks concerning the following question:

I'm trying to use get practice by writing a script that goes on a site, uses the forms on the site to choose a country, and scrape the desired information. Here's what I have.
#!C:/Perl/bin -w use LWP::UserAgent; use HTML::Parser; use strict; use HTTP::Request::Common; my %form = "country_form_0"; my $i = 0; my $agent = LWP::UserAgent->new; $agent->timeout(60); my @fields = ("country", "years1%5B%5D","c_tab","idbtable"); my $url = "http://www.census.gov/ipc/www/idb/country.php"; my @countries=("GI","GR","GL","GJ","GQ","GT","GK","GV","PU","GY","HA", +"HO","HK","HU","IC"); my @years =("2000","2001","2002","2003","2004","2005","2006","2007","2 +008","2009","2010"); my @tabs =("0","1","2"); for(1..4){ my $response = $agent-> post($url, $forms [ "country" => $countries[$i], "years1%5B%5D" => $years[0], "c_tab" => '0', "idbtable" => '0' ]); my $cont = $agent->request($response); print "$cont"; $i++; }
For some reason though, the script isn't working. I am getting the error "Global symbol @forms requires explicit name at etc..."



I'm pretty sure that I am using hashes wrong and that is causing the problem, but I'm not sure how to fix it. Any suggestions?



Similarly, how should I better write this? This can't be the best to accomplish this, right?

Thanks for all the help.





On a completely unrelated topic, what is with the painfully slow page load time? Is it just me? Or does everyone have this problem?

Replies are listed 'Best First'.
Re: Data Aggregation
by toolic (Bishop) on May 06, 2010 at 01:19 UTC
    Here is your line 20:
    my $response = $agent-> post($url, $forms
    You did not declare a variable named "forms". Are you trying to use your %form hash? From the documentation of LWP::UserAgent, it looks like you could pass an array reference to post. Try:
    my $response = $agent-> post($url, [ "country" => $countries[$i], "years1%5B%5D" => $years[0], "c_tab" => '0', "idbtable" => '0' ]);
    what is with the painfully slow page load time? Is it just me? Or does everyone have this problem?
    It's not just you.
      Well, wouldn't I have to specify the form it is going to post to?

        That is specified by your URL. An HTML form element has a method (POST in your case, so you use the post() method), an action (the url) and a set of inputs, which are handled in your array reference. No further input is needed to do the right thing here.


        print map{substr'hark, suPerJacent other l',$_,1}(11,7,6,16,5,1,15,18..23,8..10,24,17,0,12,13,3,14,2,4);
Re: Data Aggregation
by apl (Monsignor) on May 06, 2010 at 12:18 UTC
    It's a good idea to always specify use warnings;. This might have reported the problem that toolic uncovered.