The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Web::Query - Yet another scraping library like jQuery

SYNOPSIS

use Web::Query;

wq('http://www.w3.org/TR/html401/')
    ->find('div.head dt')
    ->each(sub {
        my $i = shift;
        printf("%d %s\n", $i+1, $_->text);
    });

DESCRIPTION

Web::Query is a yet another scraping framework, have a jQuery like interface.

Yes, I know Ingy's pQuery. But it's just a alpha quality. It doesn't works. Web::Query built at top of the CPAN modules, HTML::TreeBuilder::XPath, LWP::UserAgent, and HTML::Selector::XPath.

So, this module uses HTML::Selector::XPath and only supports the CSS 3 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.

$wq( '<div><p>hello</p><p>there</p></div>' )->find( 'p' );       # css selector
$wq( '<div><p>hello</p><p>there</p></div>' )->find( \'/div/p' ); # xpath selector

THIS LIBRARY IS UNDER DEVELOPMENT. ANY API MAY CHANGE WITHOUT NOTICE.

FUNCTIONS

METHODS

CONSTRUCTORS

TRAVERSING

add

Add elements to the set of matched elements.

contents

Get the immediate children of each element in the set of matched elements, including text and comment nodes.

each

Visit each nodes. $i is a counter value, 0 origin. $elem is iteration item. $_ is localized by $elem.

$q->each(sub { my ($i, $elem) = @_; ... })

end

Back to the before context like jQuery.

filter

Reduce the elements to those that pass the function's test.

$q->filter(sub { my ($i, $elem) = @_; ... })

find

Get the descendants of each element in the current set of matched elements, filtered by a selector.

my $q2 = $q->find($selector); # $selector is a CSS3 selector.

NOTE If you want to match the element itself, use "filter".

INCOMPATIBLE CHANGE From v0.14 to v0.19 (inclusive) find() also matched the element itself, which is not jQuery compatible. You can achieve that result using filter(), add() and find():

my $wq = wq('<div class="foo"><p class="foo">bar</p></div>'); # needed because we don't have a global document like jQuery does
print $wq->filter('.foo')->add($wq->find('.foo'))->as_html; # <div class="foo"><p class="foo">bar</p></div><p class="foo">bar</p>

first

Return the first matching element.

This method constructs a new Web::Query object from the first matching element.

last

Return the last matching element.

This method constructs a new Web::Query object from the last matching element.

map

Creates a new array with the results of calling a provided function on every element.

$q->map(sub { my ($i, $elem) = @_; ... })

parent

Get the parent of each element in the current set of matched elements.

prev

Get the previous node of each element in the current set of matched elements.

my $prev = $q->prev;

next

Get the next node of each element in the current set of matched elements.

my $next = $q->next;

MANIPULATION

add_class

Adds the specified class(es) to each of the set of matched elements.

# add class 'foo' to <p> elements
wq('<div><p>foo</p><p>bar</p></div>')->find('p')->add_class('foo'); 

after

Insert content, specified by the parameter, after each element in the set of matched elements.

wq('<div><p>foo</p></div>')->find('p')
                           ->after('<b>bar</b>')
                           ->end
                           ->as_html; # <div><p>foo</p><b>bar</b></div>

The content can be anything accepted by "new".

append

Insert content, specified by the parameter, to the end of each element in the set of matched elements.

wq('<div></div>')->append('<p>foo</p>')->as_html; # <div><p>foo</p></div>

The content can be anything accepted by "new".

as_html

Return the elements associated with the object as strings. If called in a scalar context, only return the string representation of the first element.

attr

Get/Set the attribute value in element.

my $attr = $q->attr($name);

$q->attr($name, $val);

tagname

Get/Set the tag name of elements.

my $name = $q->tagname;

$q->tagname($new_name);

before

Insert content, specified by the parameter, before each element in the set of matched elements.

wq('<div><p>foo</p></div>')->find('p')
                           ->before('<b>bar</b>')
                           ->end
                           ->as_html; # <div><b>bar</b><p>foo</p></div>

The content can be anything accepted by "new".

clone

Create a deep copy of the set of matched elements.

detach

Remove the set of matched elements from the DOM.

has_class

Determine whether any of the matched elements are assigned the given class.

html

Get/Set the innerHTML.

my @html = $q->html();

my $html = $q->html(); # 1st matching element only

$q->html('<p>foo</p>');

insert_before

Insert every element in the set of matched elements before the target.

insert_after

Insert every element in the set of matched elements after the target.

prepend

Insert content, specified by the parameter, to the beginning of each element in the set of matched elements.

remove

Delete the elements associated with the object from the DOM.

# remove all <blink> tags from the document
$q->find('blink')->remove;

remove_class

Remove a single class, multiple classes, or all classes from each element in the set of matched elements.

replace_with

Replace the elements of the object with the provided replacement. The replacement can be a string, a Web::Query object or an anonymous function. The anonymous function is passed the index of the current node and the node itself (with is also localized as $_).

my $q = wq( '<p><b>Abra</b><i>cada</i><u>bra</u></p>' );

$q->find('b')->replace_with('<a>Ocus</a>);
    # <p><a>Ocus</a><i>cada</i><u>bra</u></p>

$q->find('u')->replace_with($q->find('b'));
    # <p><i>cada</i><b>Abra</b></p>

$q->find('i')->replace_with(sub{ 
    my $name = $_->text;
    return "<$name></$name>";
});
    # <p><b>Abra</b><cada></cada><u>bra</u></p>

size

Return the number of elements in the Web::Query object.

wq('<div><p>foo</p><p>bar</p></div>')->find('p')->size; # 2

text

Get/Set the text.

my @text = $q->text();

my $text = $q->text(); # 1st matching element only

$q->text('text');

If called in a scalar context, only return the string representation of the first element

OTHERS

HOW DO I CUSTOMIZE USER AGENT?

You can specify your own instance of LWP::UserAgent.

$Web::Query::UserAgent = LWP::UserAgent->new( agent => 'Mozilla/5.0' );

INCOMPATIBLE CHANGES

AUTHOR

Tokuhiro Matsuno

SEE ALSO

pQuery

LICENSE

Copyright (C) Tokuhiro Matsuno

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.