I'm trying to write a program that will create a data file using information retrieved from the internet. Note that I am pretty new to Perl; I am used to programming in C and Java and have decided to use Perl in this case because of its easy to use libraries for automated internet interaction. My original code is this:
#!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
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:
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
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.altavista.com/ Couldn't find the match-string in the response
Here is my alteration (the original is the last example at the link above):
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"; }
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.

In reply to program fails to get response that should be returned by UserAgent->post by no1uno

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.