NAME
WWW::Mechanize::Query - CSS3 selectors (or jQuery like CSS selectors) for WWW::Mechanize.
VERSION
Version 0.03
SYNOPSIS
use WWW::Mechanize::Query;
my $mech = WWW::Mechanize::Query->new( -ignore_cache => 0, -debug => 0 );
$mech->get( 'http://www.amazon.com/' );
$mech->input( 'input[type="text"][name="field-keywords"]', 'Perl' );
$mech->submit();
print $mech->at('h2.resultCount')->span->text; #prints "Showing 1 - 16 of 7,104 Results"
DESCRIPTION
This module combines WWW::Mechanize with Mojo::DOM making it possible to fill forms and scrape web with help of CSS3 selectors.
For a full list of supported CSS selectors please see Mojo::DOM::CSS.
CONSTRUCTOR
new
Creates a new WWW::Mechanize's new
object with any passed arguments.
WWW::Mechanize::Query also adds simple request caching (unless ignore_cache is set to true). Also sets Firefox as the default user-agent (if not explicitly specified).
my $mech = WWW::Mechanize::Query->new( ignore_cache => 0, agent => 'LWP' );
METHODS
Methods provided by WWW::Mechanize can be accessed directly.
Methods provided by Mojo::DOM are accessible by calling dom() method.
dom()
Parses the current content and returns a Mojo::DOM object.
my $dom = $mech->dom;
print $dom->to_xml();
at()
Parses the current content and returns a Mojo::DOM object using CSS3 selectors.
my $mech = WWW::Mechanize::Query->new();
$mech->get( 'http://www.amazon.com/' );
print $mech->at( 'div > h2' )->text;
find()
Parses the current content and returns a Mojo::DOM collection using CSS3 selectors.
my $mech = WWW::Mechanize::Query->new();
$mech->get( 'http://www.amazon.com/' );
print $mech->find( 'div > h2' )->each ( sub { print shift->all_text; } );
input()
Gets or sets Form fields using CSS3 selectors.
my $mech = WWW::Mechanize::Query->new();
$mech->get( 'http://www.imdb.com/' );
$mech->input( 'input[name="q"]', 'lost' ); #fill name
$mech->input( 'select[name="s"]', 'ep' ); #select "TV" from drop-down list
$mech->submit();
print $mech->content;
print $mech->input( 'input[name="q"]' ); #prints "lost";
#TODO: Right now it fills out the first matching field but should be restricted to selected form.
click_link()
Posts to a URL as if a form is being submitted
my $mech = WWW::Mechanize::Query->new();
$mech->post('http://www.google.com/search?q=test'); #POSTs to http://www.google.com/search with "q"
click_link()
Checks if a HTML::Link exists and if so follows it (otherwise it returns 0)
my $mech = WWW::Mechanize::Query->new();
while (1) {
print "next page.\n";
last unless $mech->click_link(url_regex=>qr[/next/]);
}
simple_links()
Parses HTML::Link and returns simple links
my $mech = WWW::Mechanize::Query->new();
$mech->get( 'http://www.amazon.com/' );
my @links = $mech->find_all_links();
print $mech->simple_links(@links);
SEE ALSO
AUTHORS
San Kumar (robotreply at gmail)
COPYRIGHT AND LICENSE
This software is copyright (c) 2012 by San Kumar.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.