blib/lib/HTML/Scrape.pm

filter: Statementstmt 96.1% 101 / 105 Branchbran 90.6% 29 / 32 Conditioncond 66.6% 22 / 33 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 86.3% 171 / 198 CC 29 SCAR 21.2
CC
29
Cov
89%
_parser_handle_end
16.2
_parser_handle_start
7.2
_close_tag
5.0
CRAP 8.3
↑ Summary
1 package HTML::Scrape;
2  
3 3 ▶use 5.10.1;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 =head1 NAME
8  
9 HTML::Scrape - The great new HTML::Scrape!
10  
11 =head1 VERSION
12  
13 Version 0.2.0
14  
15 =cut
16  
17 3 our $VERSION = '0.2.0';
18  
19 3 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶use HTML::Tagset;
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23  
24 =head1 SYNOPSIS
25  
26 Handy helpers for common HTML scraping tasks.
27  
28 use HTML::Scrape;
29  
30 my $ids = HTML::Scrape::scrape_all_ids( $html );
31  
32 =head1 FUNCTIONS
33  
34 =head2 scrape_id( $id, $html )
35  
36 Scrapes the text of the single ID C<$id> from C<$html>.
37  
38 =cut
39  
40 sub scrape_id {
41 15 ▶ my $id = shift;
Subroutine scrape_id: called CC=1 SCAR=0.0
Pod documented
42 15 my $html = shift;
43  
44 15 my $all_ids = scrape_all_ids( $html, $id );
45  
46 15 return $all_ids->{$id};
47 }
48  
49  
50 =head2 scrape_all_ids( $html [, $specific_id ] )
51  
52 Parses the entire web page and returns all the text in a hashref keyed on ID.
53  
54 If you pass in C<$specific_id>, then only that ID will be scraped,
55 and parsing will stop once it is found. The better way to do this is by
56 calling C<scrape_id>.
57  
58 =cut
59  
60 sub scrape_all_ids {
61 30 ▶ my $html = shift;
Subroutine scrape_all_ids: called CC=4 SCAR=13.9
Pod documented
62 30 my $wanted_id = shift;
63  
64 30 my $p = HTML::Parser->new(
65 start_h => [ \&_parser_handle_start, 'self, tagname, attr, line, column' ],
66 end_h => [ \&_parser_handle_end, 'self, tagname, line, column' ],
67 text_h => [ \&_parser_handle_text, 'self, dtext' ],
68 );
69 30 $p->{stack} = [];
70 30 $p->{ids} = {};
71 30 ▶ if ( defined $wanted_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wanted_id) 27 3 30
72 27 $p->{wanted_id} = $wanted_id;
73 }
74  
75 30 $p->empty_element_tags(1);
76 30 $p->parse($html);
77 30 $p->eof;
78  
79 30 ▶ if ( !defined $wanted_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wanted_id) 3 27 30
80 # With a wanted_id, we would have stopped parsing early and left tags on the stack, so don't check.
81 3 ▶ if ( my $n = scalar @{$p->{stack}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = scalar @{$p->{"stack"};}) 1 2 3
82 1 warn "$n tag(s) unclosed at end of document.\n";
83 }
84 }
85  
86 30 return $p->{ids};
87 }
88  
89  
90 sub _parser_handle_start {
91 529 ▶ my $parser = shift;
Subroutine _parser_handle_start: called CC=7 SCAR=19.7
92 529 my $tagname = shift;
93 529 my $attr = shift;
94 529 my $line = shift;
95 529 my $column = shift;
96  
97 529 ▶ return if $HTML::Tagset::emptyElement{$tagname};
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Tagset::emptyElement{$tagname} 24 505 529
98  
99 505 my $id = $attr->{id};
100  
101 # If it's a dupe ID, warn and ignore the ID.
102 505 ▶ if ( defined($id) && exists $parser->{ids}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id and exists $parser->{"ids"}{$id}) 0 505 505
Condition66%2/3
defined $id and exists $parser->{"ids"}{$id}
!ll&&!rl&&r
3851200
MC/DC0%0/2
defined $id and exists $parser->{"ids"}{$id}
defined $id exists $parser->{"ids"}{$id}
Truth table66%2/3
A and B
A= defined $idB= exists $parser->{"ids"}{$id}
ABresult
0X0
100
111
103 0 warn "Duplicate ID $id found in <$tagname> at $line:$column\n";
104 0 $id = undef;
105 }
106  
107 505 my $stack = $parser->{stack};
108  
109 # Tags like <p> and <li> that don't have to close themselves get closed another of them comes along.
110 # For example:
111 # <ul>
112 # <li id="x">whatever
113 # <li id="y">thingy
114 # </ul>
115 505 ▶ if ( $HTML::Tagset::optionalEndTag{$tagname} && @{$stack} && $stack->[-1][0] eq $tagname ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Tagset::optionalEndTag{$tagname} and @{$stack;} and $stack->[-1][0] eq $tagname) 36 469 505
Condition83%5/6
$HTML::Tagset::optionalEndTag{$tagname} and @{$stack;} and $stack->[-1][0] eq $tagname
!ll&&!rl&&r
3808936
$HTML::Tagset::optionalEndTag{$tagname} and @{$stack;}
!ll&&!rl&&r
3800125
MC/DC66%2/3
$HTML::Tagset::optionalEndTag{$tagname} and @{$stack;} and $stack->[-1][0] eq $tagname
$HTML::Tagset::optionalEndTag{$tagname} @{$stack;} $stack->[-1][0] eq $tagname
Truth table75%3/4
A and B and C
A= $HTML::Tagset::optionalEndTag{$tagname}B= @{$stack;}C= $stack->[-1][0] eq $tagname
ABCresult
0XX0
10X0
1100
1111
116 36 my $item = pop @{$stack};
117 36 _close_tag( $parser, $item );
118 }
119  
120 505 push @{$stack}, [ $tagname, $id, '' ];
121  
122 505 return;
123 }
124  
125  
126 sub _parser_handle_end {
127 402 ▶ my $parser = shift;
Subroutine _parser_handle_end: called CC=14 SCAR=27.9
128 402 my $tagname = shift;
129 402 my $line = shift;
130 402 my $column = shift;
131  
132 402 ▶ return if $HTML::Tagset::emptyElement{$tagname};
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Tagset::emptyElement{$tagname} 13 389 402
133  
134 389 my $stack = $parser->{stack};
135  
136 # Deal with tags that close others.
137 389 ▶ if ( @{$stack} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$stack;}) 389 0 389
138 389 my $previous_item = $stack->[-1];
139 389 my $previous_tagname = $previous_item->[0];
140  
141 #warn "tagname $tagname hprase markup = " , $HTML::Tagset::isPhraseMarkup{$tagname} // 'undef', ' previous = ' . $previous_tagname;
142 my $this_tag_closes_previous_one =
143 ( $tagname ne $previous_tagname )
144 &&
145 (
146 ( ($tagname eq 'ul' || $tagname eq 'ol') && $previous_tagname eq 'li' )
147 ||
148 ( ($tagname eq 'dl') && ($previous_tagname eq 'dt' || $previous_tagname eq 'dd') )
149 ||
150 389 ▶ ( !$HTML::Tagset::isPhraseMarkup{$tagname} && $previous_tagname eq 'p' )
Condition62%15/24
$tagname ne $previous_tagname && (($tagname eq 'ul' || $tagname eq 'ol') && $previous_tagname eq 'li' || $tagname eq 'dl' && ($previous_tagname eq 'dt' || $previous_tagname eq 'dd') || !$HTML::Tagset::isPhraseMarkup{$tagname} && $previous_tagname eq 'p')
!ll&&!rl&&r
371414
$tagname eq "ul" || $tagname eq "ol" and $previous_tagname eq "li" or $tagname eq "dl" and $previous_tagname eq "dt" || $previous_tagname eq "dd" or not $HTML::Tagset::isPhraseMarkup{$tagname} and $previous_tagname eq "p"
l!l&&r!l&&!r
1044
$tagname eq "ul" || $tagname eq "ol" and $previous_tagname eq "li" or $tagname eq "dl" and $previous_tagname eq "dt" || $previous_tagname eq "dd"
l!l&&r!l&&!r
1008
$tagname eq "ul" || $tagname eq "ol" and $previous_tagname eq "li"
!ll&&!rl&&r
8010
$tagname eq "ul" or $tagname eq "ol"
l!l&&r!l&&!r
1008
$tagname eq "dl" and $previous_tagname eq "dt" || $previous_tagname eq "dd"
!ll&&!rl&&r
800
$previous_tagname eq "dt" or $previous_tagname eq "dd"
l!l&&r!l&&!r
000
not $HTML::Tagset::isPhraseMarkup{$tagname} and $previous_tagname eq "p"
!ll&&!rl&&r
044
MC/DC33%3/9
$tagname ne $previous_tagname && (($tagname eq 'ul' || $tagname eq 'ol') && $previous_tagname eq 'li' || $tagname eq 'dl' && ($previous_tagname eq 'dt' || $previous_tagname eq 'dd') || !$HTML::Tagset::isPhraseMarkup{$tagname} && $previous_tagname eq 'p')
$tagname ne $previous_tagname $tagname eq "ul" $tagname eq "ol" $previous_tagname eq "li" $tagname eq "dl" $previous_tagname eq "dt" $previous_tagname eq "dd" not $HTML::Tagset::isPhraseMarkup{$tagname} $previous_tagname eq "p"
Truth table14%4/27
A && B or C and D or E and F or G or not(H) and I
A= $tagname ne $previous_tagnameB= $tagname eq "ul"C= $tagname eq "ol"D= $previous_tagname eq "li"E= $tagname eq "dl"F= $previous_tagname eq "dt"G= $previous_tagname eq "dd"H= not $HTML::Tagset::isPhraseMarkup{$tagname}I= $previous_tagname eq "p"
ABCDEFGHIresult
0XXXXXXXX0
100X0XX0X0
100X0XX100
100X1000X0
100X100100
11X00XX0X0
11X00XX100
11X01000X0
11X0100100
10100XX0X0
10100XX100
10101000X0
1010100100
11X1XXXXX1
1011XXXXX1
100X11XXX1
100X101XX1
11X011XXX1
11X0101XX1
101011XXX1
1010101XX1
100X0XX111
100X100111
11X00XX111
11X0100111
10100XX111
1010100111
151 )
152 ;
153 389 ▶ if ( $this_tag_closes_previous_one ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_tag_closes_previous_one) 14 375 389
154 14 _close_tag( $parser, pop @{$stack} );
155 }
156 }
157  
158 389 ▶ if ( !@{$stack} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$stack;}) 0 389 389
159 0 warn "Unexpected closing </$tagname> at $line:$column\n";
160 0 return;
161 }
162 389 ▶ if ( $tagname ne $stack->[-1][0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname ne $stack->[-1][0]) 4 385 389
163 4 warn "Unexpected closing </$tagname> at $line:$column: Expecting </$stack->[-1][0]>\n";
164 4 return;
165 }
166  
167 385 _close_tag( $parser, pop @{$stack} );
168  
169 385 return;
170 }
171  
172  
173 sub _parser_handle_text {
174 833 ▶ my $parser = shift;
Subroutine _parser_handle_text: called CC=3 SCAR=11.0
175 833 my $text = shift;
176  
177 833 for my $item ( @{$parser->{stack}} ) {
178 4300 ▶ if ( $item->[1] ) { # Only accumulate text for tags with IDs.
Branch: 1 branch
BranchTrueFalseTotal
if ($item->[1]) 792 3508 4300
179 792 $item->[2] .= $text;
180 }
181 }
182  
183 833 return;
184 }
185  
186  
187 sub _close_tag {
188 435 ▶ my $parser = shift;
Subroutine _close_tag: called CC=5 SCAR=16.1
189 435 my $item = shift;
190  
191 435 my (undef, $id, $text) = @{$item};
192 435 ▶ if ( defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 109 326 435
193 109 my $keepit;
194  
195 109 ▶ if ( defined $parser->{wanted_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parser->{'wanted_id'}) { } 86 23 109
196 # We're searching for a specific ID.
197 86 ▶ if ( $id eq $parser->{wanted_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq $parser->{'wanted_id'}) { } 24 62 86
198 24 $keepit = 1;
199 24 $parser->eof;
200 }
201 else {
202 # No need to keep the text of an ID we don't care about.
203 }
204 }
205 else {
206 23 $keepit = 1;
207 }
208  
209 109 ▶ if ( $keepit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keepit) 47 62 109
210 47 $text =~ s/^\s+//;
211 47 $text =~ s/\s+$//;
212 47 $text =~ s/\s+/ /g;
213 47 $parser->{ids}{$id} = $text;
214 }
215 }
216  
217 435 return;
218 }
219  
220  
221 =head1 AUTHOR
222  
223 Andy Lester, C<< <andy at petdance.com> >>
224  
225 =head1 BUGS
226  
227 Please report any bugs or feature requests at L<https://github.com/petdance/html-scrape/issues>..
228  
229 =head1 SUPPORT
230  
231 You can find documentation for this module with the perldoc command.
232  
233 perldoc HTML::Scrape
234  
235 You can also look for information at:
236  
237 =over 4
238  
239 =item * Search CPAN
240  
241 L<https://metacpan.org/release/HTML-Scrape>
242  
243 =back
244  
245 =head1 LICENSE AND COPYRIGHT
246  
247 This software is Copyright (c) 2023 by Andy Lester.
248  
249 This is free software, licensed under: The Artistic License 2.0 (GPL Compatible)
250  
251 =cut
252  
253 3 1; # End of HTML::Scrape
↑ Summary