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 version 2.15
10
12 First, please note that many of these are possible just using
13 LWP::UserAgent. Since "WWW::Mechanize" is a subclass of
14 LWP::UserAgent, whatever works on "LWP::UserAgent" should work on
15 "WWW::Mechanize". See the lwpcook man page included with LWP.
16
18 Launch the WWW::Mechanize browser
19 use WWW::Mechanize;
20
21 my $mech = WWW::Mechanize->new( autocheck => 1 );
22
23 The "autocheck => 1" tells Mechanize to die if any IO fails, so you
24 don't have to manually check. It's easier that way. If you want to do
25 your own error checking, leave it out.
26
27 Fetch a page
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 Sometimes you want to dump your results directly into a file. For
36 example, there's no reason to read a JPEG into memory if you're only
37 going to write it out immediately. This can also help with memory
38 issues on large files.
39
40 $mech->get( "http://www.cpan.org/src/stable.tar.gz",
41 ":content_file" => "stable.tar.gz" );
42
43 Fetch a password-protected page
44 Generally, just call "credentials" before fetching the page.
45
46 $mech->credentials( 'admin' => 'password' );
47 $mech->get( 'http://10.11.12.13/password.html' );
48 print $mech->content();
49
51 Find all image links
52 Find all links that point to a JPEG, GIF or PNG.
53
54 my @links = $mech->find_all_links(
55 tag => "a", url_regex => qr/\.(jpe?g|gif|png)$/i );
56
57 Find all download links
58 Find all links that have the word "download" in them.
59
60 my @links = $mech->find_all_links(
61 tag => "a", text_regex => qr/\bdownload\b/i );
62
64 See what will be sent without actually sending anything
65 $mech->add_handler("request_send", sub { shift->dump; exit; });
66 $mech->get("http://www.example.com");
67
69 WWW::Mechanize
70
72 Andy Lester <andy at petdance.com>
73
75 This software is copyright (c) 2004 by Andy Lester.
76
77 This is free software; you can redistribute it and/or modify it under
78 the same terms as the Perl 5 programming language system itself.
79
80
81
82perl v5.36.0 2023-01-20 WWW::Mechanize::Cookbook(3)