.\" Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .if !\nF .nr F 0 .if \nF>0 \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} .\} .\" ======================================================================== .\" .IX Title "Web::Query 3pm" .TH Web::Query 3pm "2018-08-23" "perl v5.26.2" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Web::Query \- Yet another scraping library like jQuery .SH "VERSION" .IX Header "VERSION" version 0.39 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Web::Query; \& \& wq(\*(Aqhttp://www.w3.org/TR/html401/\*(Aq) \& \->find(\*(Aqdiv.head dt\*(Aq) \& \->each(sub { \& my $i = shift; \& printf("%d %s\en", $i+1, $_\->text); \& }); .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Web::Query is a yet another scraping framework, have a jQuery like interface. .PP Yes, I know Ingy's pQuery. But it's just a alpha quality. It doesn't works. Web::Query built at top of the \s-1CPAN\s0 modules, HTML::TreeBuilder::XPath, LWP::UserAgent, and HTML::Selector::XPath. .PP So, this module uses HTML::Selector::XPath and only supports the \s-1CSS 3\s0 selector supported by that module. Web::Query doesn't support jQuery's extended queries(yet?). If a selector is passed as a scalar ref, it'll be taken as a straight XPath expression. .PP .Vb 2 \& $wq( \*(Aq
hello
there
hello
there
foo
\*(Aq ); \& $q = Web::Query\->new( undef ); .Ve .Sp This method throw the exception on unknown \f(CW$stuff\fR. .Sp This method returns undefined value on non-successful response with \s-1URL.\s0 .Sp Currently, the only two valid options are \fIindent\fR, which will be used as the indentation string if the object is printed, and \fIno_space_compacting\fR, which will prevent the compaction of whitespace characters in text blocks. .ie n .IP "my $q = Web::Query\->new_from_element($element: HTML::Element)" 4 .el .IP "my \f(CW$q\fR = Web::Query\->new_from_element($element: HTML::Element)" 4 .IX Item "my $q = Web::Query->new_from_element($element: HTML::Element)" Create new instance of Web::Query from instance of HTML::Element. .ie n .IP """my $q = Web::Query\->new_from_html($html: Str)""" 4 .el .IP "\f(CWmy $q = Web::Query\->new_from_html($html: Str)\fR" 4 .IX Item "my $q = Web::Query->new_from_html($html: Str)" Create new instance of Web::Query from \s-1HTML.\s0 .ie n .IP "my $q = Web::Query\->new_from_url($url: Str)" 4 .el .IP "my \f(CW$q\fR = Web::Query\->new_from_url($url: Str)" 4 .IX Item "my $q = Web::Query->new_from_url($url: Str)" Create new instance of Web::Query from \s-1URL.\s0 .Sp If the response is not success(It means /^20[0\-9]$/), this method returns undefined value. .Sp You can get a last result of response, use the \f(CW$Web::Query::RESPONSE\fR. .Sp Here is a best practical code: .Sp .Vb 3 \& my $url = \*(Aqhttp://example.com/\*(Aq; \& my $q = Web::Query\->new_from_url($url) \& or die "Cannot get a resource from $url: " . Web::Query\->last_response()\->status_line; .Ve .ie n .IP "my $q = Web::Query\->new_from_file($file_name: Str)" 4 .el .IP "my \f(CW$q\fR = Web::Query\->new_from_file($file_name: Str)" 4 .IX Item "my $q = Web::Query->new_from_file($file_name: Str)" Create new instance of Web::Query from file name. .SS "\s-1TRAVERSING\s0" .IX Subsection "TRAVERSING" \fIadd\fR .IX Subsection "add" .PP Returns a new object augmented with the new element(s). .IP "add($html)" 4 .IX Item "add($html)" An \s-1HTML\s0 fragment to add to the set of matched elements. .IP "add(@elements)" 4 .IX Item "add(@elements)" One or more \f(CW@elements\fR to add to the set of matched elements. .Sp \&\f(CW@elements\fR that already are part of the set are not added a second time. .Sp .Vb 3 \& my $group = $wq\->find(\*(Aq#foo\*(Aq); # collection has 1 element \& $group = $group\->add( \*(Aq#bar\*(Aq, $wq ); # 2 elements \& $group\->add( \*(Aq#foo\*(Aq, $wq ); # still 2 elements .Ve .IP "add($wq)" 4 .IX Item "add($wq)" An existing Web::Query object to add to the set of matched elements. .ie n .IP "add($selector, $context)" 4 .el .IP "add($selector, \f(CW$context\fR)" 4 .IX Item "add($selector, $context)" \&\f(CW$selector\fR is a string representing a selector expression to find additional elements to add to the set of matched elements. .Sp \&\f(CW$context\fR is the point in the document at which the selector should begin matching .PP \fIcontents\fR .IX Subsection "contents" .PP Get the immediate children of each element in the set of matched elements, including text and comment nodes. .PP \fIeach\fR .IX Subsection "each" .PP Visit each nodes. \f(CW$i\fR is a counter value, 0 origin. \f(CW$elem\fR is iteration item. \&\f(CW$_\fR is localized by \f(CW$elem\fR. .PP .Vb 1 \& $q\->each(sub { my ($i, $elem) = @_; ... }) .Ve .PP \fIend\fR .IX Subsection "end" .PP Back to the before context like jQuery. .PP \fIfilter\fR .IX Subsection "filter" .PP Reduce the elements to those that pass the function's test. .PP .Vb 1 \& $q\->filter(sub { my ($i, $elem) = @_; ... }) .Ve .PP \fIfind\fR .IX Subsection "find" .PP Get the descendants of each element in the current set of matched elements, filtered by a selector. .PP .Vb 1 \& my $q2 = $q\->find($selector); # $selector is a CSS3 selector. .Ve .PP \&\fB\s-1NOTE\s0\fR If you want to match the element itself, use \*(L"filter\*(R". .PP \&\fB\s-1INCOMPATIBLE CHANGE\s0\fR From v0.14 to v0.19 (inclusive) \fIfind()\fR also matched the element itself, which is not jQuery compatible. You can achieve that result using \f(CW\*(C`filter()\*(C'\fR, \f(CW\*(C`add()\*(C'\fR and \f(CW\*(C`find()\*(C'\fR: .PP .Vb 2 \& my $wq = wq(\*(Aqbar
bar
bar
.Ve .PP \fIfirst\fR .IX Subsection "first" .PP Return the first matching element. .PP This method constructs a new Web::Query object from the first matching element. .PP \fIlast\fR .IX Subsection "last" .PP Return the last matching element. .PP This method constructs a new Web::Query object from the last matching element. .PP \fImatch($selector)\fR .IX Subsection "match($selector)" .PP Returns a boolean indicating if the elements match the \f(CW$selector\fR. .PP In scalar context returns only the boolean for the first element. .PP For the reverse of \f(CW\*(C`not()\*(C'\fR, see \f(CW\*(C`filter()\*(C'\fR. .PP \fInot($selector)\fR .IX Subsection "not($selector)" .PP Returns all the elements not matching the \f(CW$selector\fR. .PP .Vb 2 \& # $do_for_love will be every thing, except #that \& my $do_for_love = $wq\->find(\*(Aqthing\*(Aq)\->not(\*(Aq#that\*(Aq); .Ve .PP \fIand_back\fR .IX Subsection "and_back" .PP Add the previous set of elements to the current one. .PP .Vb 2 \& # get the h1 plus everything until the next h1 \& $wq\->find(\*(Aqh1\*(Aq)\->next_until(\*(Aqh1\*(Aq)\->and_back; .Ve .PP \fImap\fR .IX Subsection "map" .PP Creates a new array with the results of calling a provided function on every element. .PP .Vb 1 \& $q\->map(sub { my ($i, $elem) = @_; ... }) .Ve .PP \fIparent\fR .IX Subsection "parent" .PP Get the parent of each element in the current set of matched elements. .PP \fIprev\fR .IX Subsection "prev" .PP Get the previous node of each element in the current set of matched elements. .PP .Vb 1 \& my $prev = $q\->prev; .Ve .PP \fInext\fR .IX Subsection "next" .PP Get the next node of each element in the current set of matched elements. .PP .Vb 1 \& my $next = $q\->next; .Ve .PP \fInext_until( \f(CI$selector\fI )\fR .IX Subsection "next_until( $selector )" .PP Get all subsequent siblings, up to (but not including) the next node matched \f(CW$selector\fR. .SS "\s-1MANIPULATION\s0" .IX Subsection "MANIPULATION" \fIadd_class\fR .IX Subsection "add_class" .PP Adds the specified class(es) to each of the set of matched elements. .PP .Vb 2 \& # add class \*(Aqfoo\*(Aq toelements \& wq(\*(Aq
foo
bar
foo
foo
barfoo
\*(Aq)\->as_html; #foo
foo
bar
foo
!bar
.Ve .PP \fI\f(CI\*(C` attr \*(C'\fI\fR .IX Subsection " attr " .PP Get/set attribute values. .PP In getter mode, it'll return either the values of the attribute for all elements of the set, or only the first one depending of the calling context. .PP .Vb 2 \& my @values = $q\->attr(\*(Aqstyle\*(Aq); # style of all elements \& my $first_value = $q\->attr(\*(Aqstyle\*(Aq); # style of first element .Ve .PP In setter mode, it'll set attributes value for all elements, and return back the original object for easy chaining. .PP .Vb 1 \& $q\->attr( \*(Aqalt\*(Aq => \*(Aqa picture\*(Aq )\->find( ... ); \& \& # can pass more than 1 element too \& $q\->attr( alt => \*(Aqa picture\*(Aq, src => \*(Aqfile:///...\*(Aq ); .Ve .PP The value passed for an attribute can be a code ref. In that case, the code will be called with \f(CW$_\fR set to the current attribute value. If the code modifies \f(CW$_\fR, the attribute will be updated with the new value. .PP .Vb 1 \& $q\->attr( alt => sub { $_ ||= \*(AqA picture\*(Aq } ); .Ve .PP \fI\f(CI\*(C` id \*(C'\fI\fR .IX Subsection " id " .PP Get/set the elements's id attribute. .PP In getter mode, it behaves just like \f(CW\*(C`attr()\*(C'\fR. .PP In setter mode, it behaves like \f(CW\*(C`attr()\*(C'\fR, but with the following exceptions. .PP If the attribute value is a scalar, it'll be only assigned to the first element of the set (as ids are supposed to be unique), and the returned object will only contain that first element. .PP .Vb 1 \& my $first_element = $q\->id(\*(Aqthe_one\*(Aq); .Ve .PP It's possible to set the ids of all the elements by passing a sub to \f(CW\*(C`id()\*(C'\fR. The sub is given the same arguments as for \&\f(CW\*(C`each()\*(C'\fR, and its return value is taken to be the new id of the elements. .PP .Vb 1 \& $q\->id( sub { my $i = shift; \*(Aqfoo_\*(Aq . $i } ); .Ve .PP \fI\f(CI\*(C` name \*(C'\fI\fR .IX Subsection " name " .PP Get/set the elements's 'name' attribute. .PP .Vb 1 \& my $name = $q\->name; # equivalent to $q\->attr( \*(Aqname\*(Aq ); \& \& $q\->name( \*(Aqfoo\*(Aq ); # equivalent to $q\->attr( name => \*(Aqfoo\*(Aq ); .Ve .PP \fI\f(CI\*(C` data \*(C'\fI\fR .IX Subsection " data " .PP Get/set the elements's 'data\-*name*' attributes. .PP .Vb 1 \& my $data = $q\->data(\*(Aqfoo\*(Aq); # equivalent to $q\->attr( \*(Aqdata\-foo\*(Aq ); \& \& $q\->data( \*(Aqfoo\*(Aq => \*(Aqbar\*(Aq ); # equivalent to $q\->attr( \*(Aqdata\-foo\*(Aq => \*(Aqbar\*(Aq ); .Ve .PP \fItagname\fR .IX Subsection "tagname" .PP Get/Set the tag name of elements. .PP .Vb 1 \& my $name = $q\->tagname; \& \& $q\->tagname($new_name); .Ve .PP \fIbefore\fR .IX Subsection "before" .PP Insert content, specified by the parameter, before each element in the set of matched elements. .PP .Vb 4 \& wq(\*(Aqfoo
foo
foo
\*(Aq); .Ve .PP \fIinsert_before\fR .IX Subsection "insert_before" .PP Insert every element in the set of matched elements before the target. .PP \fIinsert_after\fR .IX Subsection "insert_after" .PP Insert every element in the set of matched elements after the target. .PP \fI\f(CI\*(C` prepend \*(C'\fI\fR .IX Subsection " prepend " .PP Insert content, specified by the parameter, to the beginning of each element in the set of matched elements. .PP \fIremove\fR .IX Subsection "remove" .PP Delete the elements associated with the object from the \s-1DOM.\s0 .PP .Vb 2 \& # remove all