Perl LWP example

From "A B C"
Revision as of 03:53, 16 September 2012 by Boris (talk | contribs) (Created page with "<div id="APB"> <div class="b1"> Perl: example code for LWP </div> {{fix}} An example of using LWP to have a program impersonate a Web browser. __TOC__ Start Komodo, the...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

Perl: example code for LWP


The contents of this page has recently been imported from an older version of this Wiki. This page may contain outdated information, information that is irrelevant for this Wiki, information that needs to be differently structured, outdated syntax, and/or broken links. Use with caution!


An example of using LWP to have a program impersonate a Web browser.


Start Komodo, then copy the code below and save it. Then run the code to make sure it works. Should any of the modules not be available, download and install them from CPAN.

 #!/usr/bin/perl
 use strict;
 use warnings;
 
 use LWP;
 my $url = "<nowiki>http://www.utoronto.ca</nowiki>";
 my $browser = LWP::UserAgent->new();
 
 my $html_doc = $browser->request(
          HTTP::Request->new(
             GET => $url
          )
        );
 
 if ($html_doc->is_success) {
    print("Title retrieved from $url: ", $html_doc->title, "\n");
 } else {
    print("$url: Awww, shucks, an error occurred.  :-(  \n");
 }
 
 exit();

Task:

  • Capture and print errors.
  • Parse something else.
  • Print the actual http get request.


   

Further reading and resources