File: 02_web_scraper.t

package info (click to toggle)
libhtml-treebuilder-libxml-perl 0.28-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 264 kB
  • sloc: perl: 533; makefile: 2; sh: 1
file content (43 lines) | stat: -rw-r--r-- 890 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
use strict;
use warnings;
use Test::More;
use HTML::TreeBuilder::LibXML;

plan skip_all => "this test requires Web::Scraper" unless eval "use Web::Scraper; 1";
plan tests => 2*3+1;

can_ok 'HTML::TreeBuilder::LibXML', 'ignore_unknown';

my $html = <<'...';
<html>
    <a href="http://wassr.jp/">wassr</a>
    <div>
        <a href="http://mixi.jp/">mixi</a>
    </div>
</html>
...

my $code = <<'...';
use Web::Scraper;

my $ret = scraper {
    process '//a', 'text[]', 'TEXT';
    process '//a', 'href[]', '@href';
    process '//div', 'div[]', scraper {
        process '//a', 'link[]', 'TEXT';
    };
}->scrape($html);
is_deeply($ret->{text}, ['wassr', 'mixi']);
is_deeply($ret->{href}, ['http://wassr.jp/', 'http://mixi.jp/']);
is_deeply($ret->{div}, [{link => ['mixi']}]);
...

&run;
HTML::TreeBuilder::LibXML->replace_original();
&run;

sub run {
    eval $code;
    die $@ if $@;
}