blib/lib/HTML/Scrape.pm

filter: Statementstmt 96.7% 120 / 124 Branchbran 89.5% 43 / 48 Conditioncond 69.0% 29 / 42 MC/DCmcdc 45.0% 9 / 20 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 86.7% 216 / 249 CC 40 SCAR 23.9
CC
40
Cov
90%
_parser_handle_end
18.6
_parser_handle_start
14.2
scrape_all_ids
5.0
CRAP 10.9
↑ 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 - Helper functions for scraping text from HTML tags
10  
11 =head1 VERSION
12  
13 Version 0.3.0
14  
15 =cut
16  
17 3 our $VERSION = '0.3.0';
18  
19 3 our $WARNINGS = 1;
20  
21 3 ▶use HTML::Parser;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 ▶use HTML::Tagset;
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25  
26 =head1 SYNOPSIS
27  
28 Handy helpers for common HTML scraping tasks.
29  
30 use HTML::Scrape;
31  
32 my $ids = HTML::Scrape::scrape_all_ids( $html );
33  
34 =head1 WARNINGS
35  
36 You can enable parsing warnings by setting C<$HTML::Scrape::WARNINGS>
37 to a true value. By default, no warnings are emitted.
38  
39 =head1 NOTES FOR FUTURE DOCS
40  
41 If a tag exists but has no content, including empty tags like C<< <hr> >>,
42 then it will have an empty string for content. This way you can test
43 for existence of these tags.
44  
45 =head1 FUNCTIONS
46  
47 =head2 scrape_id( $id, $html )
48  
49 Scrapes the text of the single ID C<$id> from C<$html>.
50  
51 =cut
52  
53 23 ▶sub scrape_id {
Subroutine scrape_id: called CC=1 SCAR=0.0
Pod documented
54 23 my $id = shift;
55 23 my $html = shift;
56  
57 23 my $all_ids = scrape_all_ids( $html, $id );
58  
59 23 return $all_ids->{$id};
60 }
61  
62  
63 =head2 scrape_all_ids( $html [, $specific_id ] )
64  
65 Parses the entire web page and returns all the text in a hashref keyed on ID.
66  
67 If you pass in C<$specific_id>, then only that ID will be scraped,
68 and parsing will stop once it is found. The better way to do this is by
69 calling C<scrape_id>.
70  
71 =cut
72  
73 47 ▶sub scrape_all_ids {
Subroutine scrape_all_ids: called CC=5 SCAR=16.1
Pod documented
74 47 my $html = shift;
75 47 my $wanted_id = shift;
76  
77 47 my $p = HTML::Parser->new(
78 start_h => [ \&_parser_handle_start, 'self, tagname, attr, line, column' ],
79 end_h => [ \&_parser_handle_end, 'self, tagname, line, column' ],
80 text_h => [ \&_parser_handle_text, 'self, dtext' ],
81 );
82 47 $p->{stack} = [];
83 47 $p->{ids} = {};
84 47 ▶ if ( defined $wanted_id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $wanted_id) 42 5 47
85 42 $p->{wanted_id} = $wanted_id;
86 }
87  
88 47 $p->empty_element_tags(1);
89 47 ▶ $p->parse($html) if defined($html);
Branch: 1 branch
BranchTrueFalseTotal
if defined $html 47 0 47
90 47 $p->eof;
91  
92 47 ▶ if ( !defined $wanted_id ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $wanted_id) 5 42 47
93 # With a wanted_id, we would have stopped parsing early and left tags on the stack, so don't check.
94 5 ▶ if ( my $n = scalar @{$p->{stack}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $n = scalar @{$p->{"stack"};}) 1 4 5
95 1 _warn( "$n tag(s) unclosed at end of document: " . join( ', ', map { $_->[0] } @{$p->{stack}} ) );
96 }
97 }
98  
99 47 return $p->{ids};
100 }
101  
102  
103 624 ▶sub _parser_handle_start {
Subroutine _parser_handle_start: called CC=14 SCAR=26.5
104 624 my $parser = shift;
105 624 my $tagname = shift;
106 624 my $attr = shift;
107 624 my $line = shift;
108 624 my $column = shift;
109  
110 624 my $id = $attr->{id};
111  
112 624 ▶ if ( $HTML::Tagset::emptyElement{$tagname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HTML::Tagset::emptyElement{$tagname}) 68 556 624
113 68 ▶ if ( $tagname eq 'br' || $tagname eq 'hr' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname eq "br" or $tagname eq "hr") 48 20 68
Condition100%3/3
$tagname eq "br" or $tagname eq "hr"
l!l&&r!l&&!r
282020
MC/DC100%2/2
$tagname eq "br" or $tagname eq "hr"
$tagname eq "br" $tagname eq "hr"
Truth table100%3/3
A or B
A= $tagname eq "br"B= $tagname eq "hr"
ABresult
1X1
011
000
114 48 _parser_handle_text( $parser, ' ' );
115 }
116  
117 68 ▶ if ( $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id) 14 54 68
118 14 ▶ if ( defined($parser->{wanted_id}) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parser->{'wanted_id'}) { } 12 2 14
119 12 ▶ if ( $id eq $parser->{wanted_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq $parser->{"wanted_id"}) 4 8 12
120 4 $parser->{ids}{$id} = '';
121 4 $parser->eof;
122 4 return;
123 }
124 }
125 else {
126 2 $parser->{ids}{$id} = '';
127 }
128 }
129  
130 64 return;
131 }
132  
133 # Add space if this tag is one that causes whitespace when rendered.
134 556 ▶ if ( $tagname eq 'br' || !$HTML::Tagset::isPhraseMarkup{$tagname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname eq "br" or not $HTML::Tagset::isPhraseMarkup{$tagname}) 365 191 556
Condition66%2/3
$tagname eq "br" or not $HTML::Tagset::isPhraseMarkup{$tagname}
l!l&&r!l&&!r
0365191
MC/DC50%1/2
$tagname eq "br" or not $HTML::Tagset::isPhraseMarkup{$tagname}
$tagname eq "br" not $HTML::Tagset::isPhraseMarkup{$tagname}
Truth table66%2/3
A or not(B)
A= $tagname eq "br"B= not $HTML::Tagset::isPhraseMarkup{$tagname}
ABresult
1X1
011
000
135 365 _parser_handle_text( $parser, ' ' );
136 }
137  
138 # If it's a dupe ID, warn and ignore the ID.
139 556 ▶ if ( defined($id) && exists $parser->{ids}{$id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id and exists $parser->{"ids"}{$id}) 0 556 556
Condition66%2/3
defined $id and exists $parser->{"ids"}{$id}
!ll&&!rl&&r
4041520
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
140 0 _warn( "Duplicate ID $id found in <$tagname> at $line:$column" );
141 0 $id = undef;
142 }
143  
144 556 my $stack = $parser->{stack};
145  
146 # Tags like <p> and <li> that don't have to close themselves get closed another of them comes along.
147 # For example:
148 # <ul>
149 # <li id="x">whatever
150 # <li id="y">thingy
151 # </ul>
152 556 ▶ 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 520 556
Condition83%5/6
$HTML::Tagset::optionalEndTag{$tagname} and @{$stack;} and $stack->[-1][0] eq $tagname
!ll&&!rl&&r
4229836
$HTML::Tagset::optionalEndTag{$tagname} and @{$stack;}
!ll&&!rl&&r
4220134
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
153 36 my $item = pop @{$stack};
154 36 _close_tag( $parser, $item );
155 }
156  
157 556 push @{$stack}, [ $tagname, $id, '' ];
158  
159 556 return;
160 }
161  
162  
163 458 ▶sub _parser_handle_end {
Subroutine _parser_handle_end: called CC=16 SCAR=29.2
164 458 my $parser = shift;
165 458 my $tagname = shift;
166 458 my $line = shift;
167 458 my $column = shift;
168  
169 458 ▶ return if $HTML::Tagset::emptyElement{$tagname};
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Tagset::emptyElement{$tagname} 22 436 458
170  
171 436 my $stack = $parser->{stack};
172  
173 # Deal with tags that close others. Implicitly close the previous tag if it's li, dt, dd or p.
174 436 ▶ if ( @{$stack} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$stack;}) 436 0 436
175 436 my $previous_item = $stack->[-1];
176 436 my $previous_tagname = $previous_item->[0];
177  
178 my $this_tag_closes_previous_one =
179 ( $tagname ne $previous_tagname )
180 &&
181 (
182 ( ($tagname eq 'ul' || $tagname eq 'ol') && $previous_tagname eq 'li' )
183 ||
184 ( ($tagname eq 'dl') && ($previous_tagname eq 'dt' || $previous_tagname eq 'dd') )
185 ||
186 436 ▶ ( !$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
418414
$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
187 )
188 ;
189 436 ▶ if ( $this_tag_closes_previous_one ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($this_tag_closes_previous_one) 14 422 436
190 14 _close_tag( $parser, pop @{$stack} );
191 }
192 }
193  
194 436 ▶ if ( !@{$stack} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (@{$stack;}) 0 436 436
195 0 _warn( "Unexpected closing </$tagname> at $line:$column" );
196 0 return;
197 }
198 436 ▶ if ( $tagname ne $stack->[-1][0] ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname ne $stack->[-1][0]) 4 432 436
199 4 _warn( "Unexpected closing </$tagname> at $line:$column: Expecting </$stack->[-1][0]>" );
200 4 return;
201 }
202  
203 432 _close_tag( $parser, pop @{$stack} );
204  
205 # Add space if this tag is one that causes whitespace when rendered.
206 432 ▶ if ( $tagname eq 'br' || !$HTML::Tagset::isPhraseMarkup{$tagname} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($tagname eq "br" or not $HTML::Tagset::isPhraseMarkup{$tagname}) 241 191 432
Condition66%2/3
$tagname eq "br" or not $HTML::Tagset::isPhraseMarkup{$tagname}
l!l&&r!l&&!r
0241191
MC/DC50%1/2
$tagname eq "br" or not $HTML::Tagset::isPhraseMarkup{$tagname}
$tagname eq "br" not $HTML::Tagset::isPhraseMarkup{$tagname}
Truth table66%2/3
A or not(B)
A= $tagname eq "br"B= not $HTML::Tagset::isPhraseMarkup{$tagname}
ABresult
1X1
011
000
207 241 _parser_handle_text( $parser, ' ' );
208 }
209  
210 432 return;
211 }
212  
213  
214 1615 ▶sub _parser_handle_text {
Subroutine _parser_handle_text: called CC=3 SCAR=11.0
215 1615 my $parser = shift;
216 1615 my $text = shift;
217  
218 1615 for my $item ( @{$parser->{stack}} ) {
219 6481 ▶ if ( $item->[1] ) { # Only accumulate text for tags with IDs.
Branch: 1 branch
BranchTrueFalseTotal
if ($item->[1]) 1356 5125 6481
220 1356 $item->[2] .= $text;
221 }
222 }
223  
224 1615 return;
225 }
226  
227  
228 482 ▶sub _close_tag {
Subroutine _close_tag: called CC=5 SCAR=16.1
229 482 my $parser = shift;
230 482 my $item = shift;
231  
232 482 my (undef, $id, $text) = @{$item};
233 482 ▶ if ( defined $id ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $id) 137 345 482
234 137 my $keepit;
235  
236 137 ▶ if ( defined $parser->{wanted_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parser->{'wanted_id'}) { } 109 28 137
237 # We're searching for a specific ID.
238 109 ▶ if ( $id eq $parser->{wanted_id} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($id eq $parser->{'wanted_id'}) { } 34 75 109
239 34 $keepit = 1;
240 34 $parser->eof;
241 }
242 else {
243 # No need to keep the text of an ID we don't care about.
244 }
245 }
246 else {
247 28 $keepit = 1;
248 }
249  
250 137 ▶ if ( $keepit ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($keepit) 62 75 137
251 62 $text =~ s/^\s+//;
252 62 $text =~ s/\s+$//;
253 62 $text =~ s/\s+/ /g;
254 62 $parser->{ids}{$id} = $text;
255 }
256 }
257  
258 482 return;
259 }
260  
261  
262 5 ▶sub _warn {
Subroutine _warn: called CC=2 SCAR=7.2
263 5 ▶ warn @_, "\n" if $WARNINGS;
Branch: 1 branch
BranchTrueFalseTotal
if $HTML::Scrape::WARNINGS 5 0 5
264  
265 5 return;
266 }
267  
268  
269 =head1 AUTHOR
270  
271 Andy Lester, C<< <andy at petdance.com> >>
272  
273 =head1 BUGS
274  
275 Please report any bugs or feature requests at L<https://github.com/petdance/html-scrape/issues>..
276  
277 =head1 SUPPORT
278  
279 You can find documentation for this module with the perldoc command.
280  
281 perldoc HTML::Scrape
282  
283 You can also look for information at:
284  
285 =over 4
286  
287 =item * Search CPAN
288  
289 L<https://metacpan.org/release/HTML-Scrape>
290  
291 =back
292  
293 =head1 LICENSE AND COPYRIGHT
294  
295 This software is Copyright (c) 2023 by Andy Lester.
296  
297 This is free software, licensed under: The Artistic License 2.0 (GPL Compatible)
298  
299 =cut
300  
301 3 1; # End of HTML::Scrape
↑ Summary