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 LWP::UserA‐
11 gent, whatever works on "LWP::UserAgent" should work on "WWW::Mecha‐
12 nize". See the lwpcook man page included with the libwww-perl distri‐
13 bution.
14
16 Launch the WWW::Mechanize browser
17
18 use WWW::Mechanize;
19
20 my $mech = WWW::Mechanize->new( autocheck => 1 );
21
22 The "autocheck => 1" tells Mechanize to die if any IO fails, so you
23 don't have to manually check. It's easier that way. If you want to do
24 your own error checking, leave it out.
25
26 Fetch a page
27
28 $mech->get( "http://search.cpan.org" );
29 print $mech->content;
30
31 "$mech->content" contains the raw HTML from the web page. It is not
32 parsed or handled in any way, at least through the "content" method.
33
34 Fetch a page into a file
35
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 only
38 going to write it out immediately. This can also help with memory
39 issues on large files.
40
41 $mech->get( "http://www.cpan.org/src/stable.tar.gz",
42 ":content_file" => "stable.tar.gz" );
43
44 Fetch a password-protected page
45
46 Generally, just call "credentials" before fetching the page.
47
48 $mech->credentials( 'admin' => 'password' );
49 $mech->get( 'http://10.11.12.13/password.html' );
50 print $mech->content();
51
53 Find all image links
54
55 Find all links that point to a JPEG, GIF or PNG.
56
57 my @links = $mech->find_all_links(
58 tag => "a", url_regex => qr/\.(jpe?g⎪gif⎪png)$/i );
59
60 Find all download links
61
62 Find all links that have the word "download" in them.
63
64 my @links = $mech->find_all_links(
65 tag => "a", text_regex => qr/\bdownload\b/i );
66
68 Check all pages on a web site
69
70 Use Abe Timmerman's WWW::CheckSite
71 <http://search.cpan.org/dist/WWW-CheckSite/>
72
74 Copyright 2005 Andy Lester "<andy@petdance.com>"
75
76 Later contributions by Peter Scott, Mark Stosberg and others.
77
78
79
80perl v5.8.8 2007-10-30 WWW::Mechanize::Cookbook(3)