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 1.91
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 the libwww-
16 perl distribution.
17
19 Launch the WWW::Mechanize browser
20 use WWW::Mechanize;
21
22 my $mech = WWW::Mechanize->new( autocheck => 1 );
23
24 The "autocheck => 1" tells Mechanize to die if any IO fails, so you
25 don't have to manually check. It's easier that way. If you want to do
26 your own error checking, leave it out.
27
28 Fetch a page
29 $mech->get( "http://search.cpan.org" );
30 print $mech->content;
31
32 "$mech->content" contains the raw HTML from the web page. It is not
33 parsed or handled in any way, at least through the "content" method.
34
35 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 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 Generally, just call "credentials" before fetching the page.
46
47 $mech->credentials( 'admin' => 'password' );
48 $mech->get( 'http://10.11.12.13/password.html' );
49 print $mech->content();
50
52 Find all image links
53 Find all links that point to a JPEG, GIF or PNG.
54
55 my @links = $mech->find_all_links(
56 tag => "a", url_regex => qr/\.(jpe?g|gif|png)$/i );
57
58 Find all download links
59 Find all links that have the word "download" in them.
60
61 my @links = $mech->find_all_links(
62 tag => "a", text_regex => qr/\bdownload\b/i );
63
65 See what will be sent without actually sending anything
66 $mech->add_handler("request_send", sub { shift->dump; exit; });
67 $mech->get("http://www.example.com");
68
70 WWW::Mechanize
71
73 Andy Lester <andy at petdance.com>
74
76 This software is copyright (c) 2004-2016 by Andy Lester.
77
78 This is free software; you can redistribute it and/or modify it under
79 the same terms as the Perl 5 programming language system itself.
80
81
82
83perl v5.28.1 2019-01-10 WWW::Mechanize::Cookbook(3)