3 WWW::Mechanize::Cookbook - Recipes for using WWW::Mechanize
7 First, please note that many of these are possible just using
8 L<LWP::UserAgent>. Since C<WWW::Mechanize> is a subclass of
9 L<LWP::UserAgent>, whatever works on C<LWP::UserAgent> should work
10 on C<WWW::Mechanize>. See the L<lwpcook> man page included with
11 the L<libwww-perl> distribution.
15 =head2 Launch the WWW::Mechanize browser
19 my $mech = WWW::Mechanize->new( autocheck => 1 );
21 The C<< autocheck => 1 >> tells Mechanize to die if any IO fails,
22 so you don't have to manually check. It's easier that way. If you
23 want to do your own error checking, leave it out.
27 $mech->get( "http://search.cpan.org" );
30 C<< $mech->content >> contains the raw HTML from the web page. It
31 is not parsed or handled in any way, at least through the C<content>
34 =head2 Fetch a page into a file
36 Sometimes you want to dump your results directly into a file. For
37 example, there's no reason to read a JPEG into memory if you're
38 only going to write it out immediately. This can also help with
39 memory issues on large files.
41 $mech->get( "http://www.cpan.org/src/stable.tar.gz",
42 ":content_file" => "stable.tar.gz" );
44 =head2 Fetch a password-protected page
46 Generally, just call C<credentials> before fetching the page.
48 $mech->credentials( 'admin' => 'password' );
49 $mech->get( 'http://10.11.12.13/password.html' );
50 print $mech->content();
54 =head2 Find all image links
56 Find all links that point to a JPEG, GIF or PNG.
58 my @links = $mech->find_all_links(
59 tag => "a", url_regex => qr/\.(jpe?g|gif|png)$/i );
61 =head2 Find all download links
63 Find all links that have the word "download" in them.
65 my @links = $mech->find_all_links(
66 tag => "a", text_regex => qr/\bdownload\b/i );
70 =head2 Check all pages on a web site
72 Use Abe Timmerman's L<WWW::CheckSite>
73 L<http://search.cpan.org/dist/WWW-CheckSite/>
77 Copyright 2005 Andy Lester C<< <andy@petdance.com> >>
79 Later contributions by Peter Scott, Mark Stosberg and others.