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