no1uno has asked for the wisdom of the Perl Monks concerning the following question:
This was mostly a copy/paste/replace operation using a bit of the Perl tutorial off Perl.org and the first two pages from this site. Here is my program's output:#!c:\perl\bin -w use strict; use warnings; use LWP 5.8; my $browser = LWP::UserAgent->new; $browser->timeout(60); push @{ $browser->requests_redirectable }, 'POST'; my $cityInfoMainPageURL = 'http://www.ihoz.com/ilist.html'; my $distanceFinderURL = 'http://www.randmcnally.com/rmc/directions/d +irGetMileageInput.jsp'; my $cities = $browser->get($cityInfoMainPageURL); my $distIn = $browser->get($distanceFinderURL); die ("Can't get $cityInfoMainPageURL -- ", $cities->status_line) unless $cities->is_success; die ("Can't get $distanceFinderURL -- ", $distIn->status_line) unless $distIn->is_success; print ($distIn->base, "\n"); print ($cities->base, "\n"); my $strtCity = 'Miami'; my $strtState = 'FL'; my $destCity = 'Albany'; my $destState = 'NY'; my $cityResponse = $browser->post ( $distanceFinderURL, [ 'txtStartCity' => $strtCity, 'txtStartState' => $strtState, 'txtDestCity' => $destCity, 'txtDestState' => $destState, ] ); die ("error submiting form") unless $cityResponse->is_success; print ($cityResponse->status_line, "\n"); print ($cityResponse->base, "\n"); $cityResponse->content =~ /Driving Distance:.*([1-9][0-9]*|0) miles/; print (1); # this used to be "print ($1)" but the warning # about the undefined variable was annoying
obviously, most of the output is for debugging the fact that my post command doesn't seem to be posting. To try and narrow the source of the problem, I tried running an altered version of the sample i found here and it produced this output:http://www.randmcnally.com/rmc/directions/dirGetMileageInput.jsp http://www.ihoz.com/ilist.html 200 OK http://www.randmcnally.com/rmc/directions/dirGetMileageInput.jsp 1
Here is my alteration (the original is the last example at the link above):http://www.altavista.com/ Couldn't find the match-string in the response
Note that the main alterations are for debugging, the switch to a more lenient regex, and an accomodation for the apparent fact that altavista has made some changes since the sample was written. Since it appears to me that so long as the sample is good code my program should work, I suspect that the problem might not be with the program but with the computer. I have ZoneAlarm, MacAffee (or maybe not), and SafeEyes, and am running Windows XP.use strict; use warnings; use LWP 5.64; my $browser = LWP::UserAgent->new; push @{ $browser->requests_redirectable }, 'POST'; my $word = 'tarragon'; my $url = 'http://www.altavista.com/'; my $response = $browser->post( $url, [ 'q' => $word, # the Altavista query string # 'pg' => 'q', 'avkw' => 'tgz', 'kl' => 'XX', ] ); die "$url error!!: ", $response->status_line unless $response->is_success; die "Weird content type at $url -- ", $response->content_type unless $response->content_type eq 'text/html'; print ($response->base . "\n"); if( $response->content =~ m{(AltaVista|Alta Vista).* .*found.* .*([0 +-9,]+).* .*results} ) { # The substring will be like "AltaVista found 2,345 results" print "$word: $2\n"; } else { print "Couldn't find the match-string in the response\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: program fails to get response that should be returned by UserAgent->post
by Cody Pendant (Prior) on Jul 11, 2007 at 05:14 UTC | |
|
Re: program fails to get response that should be returned by UserAgent->post
by Cody Pendant (Prior) on Jul 11, 2007 at 05:29 UTC | |
|
Re: program fails to get response that should be returned by UserAgent->post
by ww (Archbishop) on Jul 11, 2007 at 14:48 UTC |