HTML::Query

jQuery-like selection queries for HTML::Element
Download

HTML::Query Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Publisher Name:
  • Andy Wardley
  • Publisher web site:
  • http://search.cpan.org/~abw/

HTML::Query Tags


HTML::Query Description

jQuery-like selection queries for HTML::Element HTML::Query is a Perl module, an add-on for the HTML::Tree module set. It provides a simple way to select one or more elements from a tree using a query syntax inspired by jQuery. This selector syntax will be reassuringly familiar to anyone who has ever written a CSS selector.HTML::Query is not an attempt to provide a complete (or even near-complete) implementation of jQuery in Perl (see Ingy's pQuery module for a more ambitious attempt at that). Rather, it borrows some of the tried and tested selector syntax from jQuery (and CSS) that can easily be mapped onto the look_down() method provided by the HTML::Element module.SYNOPSISCreating an HTML::Query object using the Query() constructor subroutine: use HTML::Query 'Query'; # using named parameters $q = Query( text => $text ); # HTML text $q = Query( file => $file ); # HTML file $q = Query( tree => $tree ); # HTML::Element object $q = Query( query => $query ); # HTML::Query object $q = Query( text => $text1, # or any combination text => $text2, # of the above file => $file1, file => $file2, tree => $tree, query => $query, ); # passing elements as positional arguments $q = Query( $tree ); # HTML::Element object(s) $q = Query( $tree1, $tree2, $tree3, ... ); # or from one or more existing queries $q = Query( $query1 ); # HTML::Query object(s) $q = Query( $query1, $query2, $query3, ... ); # or a mixture $q = Query( $tree1, $query1, $tree2, $query2 ); # the final argument (in all cases) can be a selector my $spec = 'ul.menu li a'; # .... $q = Query( $tree, $spec ); $q = Query( $query, $spec ); $q = Query( $tree1, $tree2, $query1, $query2, $spec ); $q = Query( text => $text, $spec ); $q = Query( file => $file, $spec ); $q = Query( tree => $tree, $spec ); $q = Query( query => $query, $spec ); $q = Query( text => $text, file => $file, # ...etc... $spec );Or using the OO new() constructor method (which the Query() subroutine maps onto): use HTML::Query; $q = HTML::Query->new( # accepts the same arguments as Query() )Or by monkey-patching a query() method into HTML::Element. use HTML::Query 'query'; # note lower case 'q' use HTML::TreeBuilder; # build a tree my $tree = HTML::TreeBuilder->new; $tree->parse_file($filename); # call the query() method on any element my $query = $tree->query($spec); Once you have a query, you can start selecting elements: @r = $q->query('a'); # all ... elements @r = $q->query('a#menu'); # all with "menu" id @r = $q->query('#menu'); # all elements with "menu" id @r = $q->query('a.menu'); # all with "menu" class @r = $q->query('.menu'); # all elements with "menu" class @r = $q->query('a'); # all with 'href' attr @r = $q->query('a'); # all with 'href="foo"' attr # you can specify elements within elements... @r = $q->query('ul.menu li a'); # ...... # and use commas to delimit multiple path specs for different elements @r = $q->query('table tr td a, ul.menu li a, form input'); # query() in scalar context returns a new query $r = $q->query('table'); # find all tables $s = $r->query('tr'); # find all rows in all those tables $t = $s->query('td'); # and all cells in those rows...Inspecting query elements: # get number of elements in query my $size = $q->size # get first/last element in query my $first = $q->first; my $last = $q->last; # convert query to list or list ref of HTML::Element objects my $list = $q->list; # list ref in scalar context my @list = $q->list; # list in list contextAll other methods are mapped onto the HTML::Element objects in the query: print $query->as_trimmed_text; # print trimmed text for each element print $query->as_HTML; # print each element as HTML $query->delete; # call delete() on each element Requirements: · Perl


HTML::Query Related Software