LWP request example
Shows how to load a text file
Another good resource: Web Client Programming with Perl (oreilly.com).
# load LWP library: use LWP::UserAgent; use HTML::Parse; # define a URL my $url = 'http://www.jonasjohn.de'; # create UserAgent object my $ua = new LWP::UserAgent; # set a user agent (browser-id) # $ua->agent('Mozilla/5.5 (compatible; MSIE 5.5; Windows NT 5.1)'); # timeout: $ua->timeout(15); # proceed the request: my $request = HTTP::Request->new('GET'); $request->url($url); my $response = $ua->request($request); # # responses: # # response code (like 200, 404, etc) my $code = $response->code; # headers (Server: Apache, Content-Type: text/html, ...) my $headers = $response->headers_as_string; # HTML body: my $body = $response->content; # print the website content: # print $body; # do some parsing: my $parsed_html = HTML::Parse::parse_html($body); for (@{ $parsed_html->extract_links(qw(a body img)) }) { # extract all links (a, body, img) my ($link) = @$_; # print link: print $link . "\n"; }
Snippet Details
-
AuthorJonas
-
LicensePublic Domain
-
LanguagePerl
-
Created05/29/2006
-
Updated05/29/2006
-
Tagsnetwork, http, connections
Sorry folks, comments have been deactivated for now due to the large amount of spam.
Please try to post your questions or problems on a related programming board, a suitable mailing list, a programming chat-room,
or use a QA website like stackoverflow because I'm usually too busy to answer any mails related
to my code snippets. Therefore please just mail me if you found a serious bug... Thank you!
Older comments:
yoann February 15, 2011 at 02:38
$request->url($x);
that isn't url but uri
that isn't url but uri