| 1 | package App::scrape; | ||||||||||||||||||||||||||||||||||
| 2 | 4 | ▶ | use strict; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 3 | 4 | ▶ | use URI; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 4 | 4 | ▶ | use HTML::TreeBuilder::XPath; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 5 | 4 | ▶ | use HTML::Selector::XPath 'selector_to_xpath'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 6 | 4 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||
| 8 | 4 | our $VERSION = '0.08'; | |||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||
| 10 | 4 | our @EXPORT_OK = qw<scrape>; | |||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||
| 12 | =head1 NAME | ||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||
| 14 | App::scrape - simple HTML scraping | ||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||
| 16 | =head1 ABSTRACT | ||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||
| 18 | This is a simple module to extract data from HTML by | ||||||||||||||||||||||||||||||||||
| 19 | specifying CSS3 or XPath selectors. | ||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||
| 21 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||
| 23 | use App::scrape 'scrape'; | ||||||||||||||||||||||||||||||||||
| 24 | use LWP::Simple 'get'; | ||||||||||||||||||||||||||||||||||
| 25 | use Data::Dumper; | ||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||
| 27 | my $html = get('http://perlmonks.org'); | ||||||||||||||||||||||||||||||||||
| 28 | my @posts = scrape( | ||||||||||||||||||||||||||||||||||
| 29 | $html, | ||||||||||||||||||||||||||||||||||
| 30 | ['a','a@href'], | ||||||||||||||||||||||||||||||||||
| 31 | { | ||||||||||||||||||||||||||||||||||
| 32 | absolute => [qw[href src rel]], | ||||||||||||||||||||||||||||||||||
| 33 | base => 'http://perlmonks.org', | ||||||||||||||||||||||||||||||||||
| 34 | }, | ||||||||||||||||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||||||||||||||
| 36 | print Dumper \@posts; | ||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||
| 38 | my @posts = scrape( | ||||||||||||||||||||||||||||||||||
| 39 | $html, | ||||||||||||||||||||||||||||||||||
| 40 | { | ||||||||||||||||||||||||||||||||||
| 41 | title => 'a', | ||||||||||||||||||||||||||||||||||
| 42 | url => 'a@href', | ||||||||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||||||||
| 44 | { | ||||||||||||||||||||||||||||||||||
| 45 | absolute => [qw[href src rel]], | ||||||||||||||||||||||||||||||||||
| 46 | base => 'http://perlmonks.org', | ||||||||||||||||||||||||||||||||||
| 47 | }, | ||||||||||||||||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||||||||||||||||
| 49 | print Dumper \@posts; | ||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||
| 51 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||||||||
| 53 | This module implements yet another scraping engine | ||||||||||||||||||||||||||||||||||
| 54 | to extract data from HTML. | ||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||
| 56 | This engine does not (yet) support nested data | ||||||||||||||||||||||||||||||||||
| 57 | structures. For an engine that supports nesting, see | ||||||||||||||||||||||||||||||||||
| 58 | L<Web::Scraper>. | ||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||
| 60 | =cut | ||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||
| 62 | 6 | ▶ | sub scrape { | ||||||||||||||||||||||||||||||||
|
Subroutine
scrape: called CC=20 SCAR=30.2
Pod
undocumented
| |||||||||||||||||||||||||||||||||||
| 63 | 6 | my ($html, $selectors, $options) = @_; | |||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||
| 65 | 6 | ▶ | $options ||= {}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options ||= {}
MC/DC0%0/1
$options ||= {}
$options
Truth table50%1/2
A
A= $options
| |||||||||||||||||||||||||||||||||||
| 66 | 6 | my $delete_tree; | |||||||||||||||||||||||||||||||||
| 67 | 6 | ▶ | if (! ref $options->{tree}) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 68 | 4 | $options->{tree} = HTML::TreeBuilder::XPath->new; | |||||||||||||||||||||||||||||||||
| 69 | 4 | $options->{tree}->parse($html); | |||||||||||||||||||||||||||||||||
| 70 | 4 | $options->{tree}->eof; | |||||||||||||||||||||||||||||||||
| 71 | 4 | $delete_tree = 1; | |||||||||||||||||||||||||||||||||
| 72 | }; | ||||||||||||||||||||||||||||||||||
| 73 | 6 | my $tree = $options->{tree}; | |||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||
| 75 | 6 | ▶ | $options->{make_uri} ||= {}; | ||||||||||||||||||||||||||||||||
|
Condition50%1/2
$options->{"make_uri"} ||= {}
MC/DC0%0/1
$options->{"make_uri"} ||= {}
$options->{"make_uri"}
Truth table50%1/2
A
A= $options->{"make_uri"}
| |||||||||||||||||||||||||||||||||||
| 76 | 6 | my %make_uri = %{$options->{make_uri}}; | |||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||
| 78 | # now fetch all "rows" from the page. We do this once to avoid | ||||||||||||||||||||||||||||||||||
| 79 | # fetching a page multiple times | ||||||||||||||||||||||||||||||||||
| 80 | 6 | my @rows; | |||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||
| 82 | 6 | my %known_uri = ( | |||||||||||||||||||||||||||||||||
| 83 | 'href' => 1, # a@href | ||||||||||||||||||||||||||||||||||
| 84 | 'src' => 1, # img@src , script@src | ||||||||||||||||||||||||||||||||||
| 85 | ); | ||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||
| 87 | 6 | my @selectors; | |||||||||||||||||||||||||||||||||
| 88 | 6 | ▶ | if (ref $selectors eq 'ARRAY') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 89 | 4 | @selectors = @$selectors | |||||||||||||||||||||||||||||||||
| 90 | } else { | ||||||||||||||||||||||||||||||||||
| 91 | 2 | @selectors = map { $selectors->{ $_ } } sort keys %$selectors; | |||||||||||||||||||||||||||||||||
| 92 | }; | ||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||
| 94 | 6 | my $rowidx=0; | |||||||||||||||||||||||||||||||||
| 95 | 6 | my $found_max = 0; | |||||||||||||||||||||||||||||||||
| 96 | 6 | for my $selector (@selectors) { | |||||||||||||||||||||||||||||||||
| 97 | 6 | my ($attr); | |||||||||||||||||||||||||||||||||
| 98 | 6 | my $s = $selector; | |||||||||||||||||||||||||||||||||
| 99 | 6 | ▶ | if ($selector =~ s!/?\@(\w+)$!!) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 100 | 3 | $attr = $1; | |||||||||||||||||||||||||||||||||
| 101 | }; | ||||||||||||||||||||||||||||||||||
| 102 | 6 | ▶ | if ($selector !~ m!^\.?/!) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 103 | 5 | $selector = selector_to_xpath( $selector ); | |||||||||||||||||||||||||||||||||
| 104 | }; | ||||||||||||||||||||||||||||||||||
| 105 | # We always make the selector relative to the current node: | ||||||||||||||||||||||||||||||||||
| 106 | 6 | ▶ | $selector = ".$selector" unless $selector =~ /^\./; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 107 | 6 | my @nodes; | |||||||||||||||||||||||||||||||||
| 108 | 6 | ▶ | if (! defined $attr) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 109 | 3 | @nodes = map { $_->as_trimmed_text } $tree->findnodes($selector); | |||||||||||||||||||||||||||||||||
| 110 | } else { | ||||||||||||||||||||||||||||||||||
| 111 | 3 | ▶ | $make_uri{ $rowidx } ||= (($known_uri{ lc $attr }) and ! $options->{no_known_uri}); | ||||||||||||||||||||||||||||||||
|
Condition33%2/6
$make_uri{$rowidx} ||= $known_uri{lc $attr} && !$options->{"no_known_uri"}
$known_uri{lc $attr} && !$options->{'no_known_uri'}
MC/DC0%0/3
$make_uri{$rowidx} ||= $known_uri{lc $attr} && !$options->{"no_known_uri"}
$make_uri{$rowidx}
$known_uri{lc $attr}
!$options->{'no_known_uri'}
Truth table0%0/4
A ||= B && not(C)
A= $make_uri{$rowidx}B= $known_uri{lc $attr}C= !$options->{'no_known_uri'}
| |||||||||||||||||||||||||||||||||||
| 112 | 3 | @nodes = $tree->findvalues("$selector/\@$attr"); | |||||||||||||||||||||||||||||||||
| 113 | }; | ||||||||||||||||||||||||||||||||||
| 114 | 6 | ▶ | if ($make_uri{ $rowidx }) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 115 | 3 | @nodes = map { URI->new_abs( $_, $options->{base} )->as_string } @nodes; | |||||||||||||||||||||||||||||||||
| 116 | }; | ||||||||||||||||||||||||||||||||||
| 117 | 6 | ▶ | if( $found_max < @nodes) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 118 | 5 | $found_max = @nodes | |||||||||||||||||||||||||||||||||
| 119 | }; | ||||||||||||||||||||||||||||||||||
| 120 | 6 | push @rows, \@nodes; | |||||||||||||||||||||||||||||||||
| 121 | 6 | $rowidx++; | |||||||||||||||||||||||||||||||||
| 122 | }; | ||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||
| 124 | # Now convert the result from rows to columns | ||||||||||||||||||||||||||||||||||
| 125 | 6 | my @result; | |||||||||||||||||||||||||||||||||
| 126 | 6 | for my $idx (0.. $found_max-1) { | |||||||||||||||||||||||||||||||||
| 127 | push @result, [ map { | ||||||||||||||||||||||||||||||||||
| 128 | 14 | $rows[$_]->[$idx] | |||||||||||||||||||||||||||||||||
| 129 | } 0..$#rows ]; | ||||||||||||||||||||||||||||||||||
| 130 | }; | ||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||
| 132 | # Now check what the user wants, array or hash: | ||||||||||||||||||||||||||||||||||
| 133 | 6 | ▶ | if( ref $selectors eq 'HASH') { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 134 | @result = map { | ||||||||||||||||||||||||||||||||||
| 135 | 2 | my $arr = $_; | |||||||||||||||||||||||||||||||||
| 136 | 4 | my $i = 0; | |||||||||||||||||||||||||||||||||
| 137 | 4 | my @keys = sort { $a cmp $b } keys( %$selectors ); | |||||||||||||||||||||||||||||||||
| 138 | $_ = +{ | ||||||||||||||||||||||||||||||||||
| 139 | 4 | map { $_ => $arr->[$i++] } @keys | |||||||||||||||||||||||||||||||||
| 140 | }; | ||||||||||||||||||||||||||||||||||
| 141 | $_->{ $options->{url_field}} = $options->{base} | ||||||||||||||||||||||||||||||||||
| 142 | 4 | ▶ | if( $options->{url_field} ); | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 143 | 4 | $_ | |||||||||||||||||||||||||||||||||
| 144 | } @result | ||||||||||||||||||||||||||||||||||
| 145 | } else { | ||||||||||||||||||||||||||||||||||
| 146 | 4 | ▶ | if( $options->{url_field} ) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 147 | 0 | for my $row (@result) { | |||||||||||||||||||||||||||||||||
| 148 | 0 | unshift @$row, $options->{base}; | |||||||||||||||||||||||||||||||||
| 149 | }; | ||||||||||||||||||||||||||||||||||
| 150 | }; | ||||||||||||||||||||||||||||||||||
| 151 | }; | ||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||
| 153 | 6 | ▶ | $tree->delete | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 154 | if $delete_tree; | ||||||||||||||||||||||||||||||||||
| 155 | @result | ||||||||||||||||||||||||||||||||||
| 156 | 6 | }; | |||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||
| 158 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||
| 160 | L<Web::Scraper> - the scraper inspiring this module | ||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||
| 162 | =head1 REPOSITORY | ||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||
| 164 | The public repository of this module is | ||||||||||||||||||||||||||||||||||
| 165 | L<http://github.com/Corion/App-scrape>. | ||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||
| 167 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||
| 169 | The public support forum of this program is | ||||||||||||||||||||||||||||||||||
| 170 | L<http://perlmonks.org/>. | ||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||
| 172 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||
| 174 | Max Maischein C<corion@cpan.org> | ||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||
| 176 | =head1 COPYRIGHT (c) | ||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||
| 178 | Copyright 2011-2011 by Max Maischein C<corion@cpan.org>. | ||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||
| 180 | =head1 LICENSE | ||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||
| 182 | This module is released under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||
| 184 | =cut | ||||||||||||||||||||||||||||||||||