Crawl.pm

filter: Statementstmt 26.9% 24 / 89 Branchbran 0.0% 0 / 48 Conditioncond 0.0% 0 / 26 MC/DCmcdc 0.0% 0 / 16 Subroutinesub 70.0% 7 / 10 Podpod 100% 2 / 2 totaltotal 17.2% 33 / 191 CC 34 SCAR 63.8
CC
34
Cov
15%
crawl
870.0
_fetch_page
30.0
new
6.0
CRAP 590.7
↑ Summary Crawl/Auto.pm »
1 package WWW::Crawl;
2
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5
6 3 ▶use HTTP::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use JSON::PP;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use IO::Uncompress::Gunzip qw(gunzip $GunzipError);
Subroutine BEGIN: called CC=1 SCAR=0.0
11
12 3 our $VERSION = '0.5';
13 # $VERSION = eval $VERSION;
14
15 # TODO:
16 # 1 - Use HTML Parser instead of regexps
17 # - we don't do this as it doesn't parse JS scripts and files
18 #
19
20 0 ▶sub new {
Subroutine new: not called CC=2 SCAR=17.9
Pod documented
21 0 my $class = shift;
22 0 my %attrs = @_;
23
24 0 ▶ $attrs{'agent'} //= "Perl-WWW-Crawl-$VERSION";
Condition0%0/2
$attrs{"agent"} //= "Perl-WWW-Crawl-$WWW::Crawl::VERSION"
l!l
00
MC/DC0%0/1
$attrs{"agent"} //= "Perl-WWW-Crawl-$WWW::Crawl::VERSION"
$attrs{"agent"}
Truth table0%0/2
A
A= $attrs{"agent"}
Aresult
11
00
25
26 $attrs{'http'} = HTTP::Tiny->new(
27 0 'agent' => $attrs{'agent'},
28 );
29
30 0 return bless \%attrs, $class;
31 }
32
33 0 ▶sub crawl {
Subroutine crawl: not called CC=29 SCAR=67.7
Pod documented
34 0 my ($self, $url, $callback) = @_;
35
36 0 ▶ $url = "https://$url" if $url =~ /^www/;
Branch: 1 branch
BranchTrueFalseTotal
if $url =~ /^www/ 0 0 0
37 0 my $uri = URI->new($url);
38 0 ▶ croak "WWW::Crawl: No valid URI" unless $uri;
Branch: 1 branch
BranchTrueFalseTotal
unless $uri 0 0 0
39
40 0 my (%links, %parsed);
41 0 $links{$url} = 1;
42
43 0 my $page;
44 0 my $flag = 1;
45 0 ▶ while (scalar keys %links and $flag) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar keys %links and $flag) 0 0 0
Condition0%0/3
scalar keys %links and $flag
!ll&&!rl&&r
000
MC/DC0%0/2
scalar keys %links and $flag
scalar keys %links $flag
Truth table0%0/3
A and B
A= scalar keys %linksB= $flag
ABresult
0X0
100
111
46 0 my $url = (keys(%links))[0];
47 0 delete $links{$url};
48
49 0 ▶ next if $parsed{$url};
Branch: 1 branch
BranchTrueFalseTotal
if $parsed{$url} 0 0 0
50 0 $parsed{$url}++;
51
52 0 my $resp = $self->_fetch_page($url);
53 0 ▶ next if $resp->{'status'} == 404;
Branch: 1 branch
BranchTrueFalseTotal
if $resp->{"status"} == 404 0 0 0
54 0 ▶ if (!$resp->{'success'}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($resp->{"success"}) 0 0 0
55 0 warn "WWW::Crawl: HTTP Response " . $resp->{'status'} . " - " . $resp->{'reason'} . "\n";
56 0 next;
57 }
58
59 0 $page = $resp->{'content'};
60
61 #print "\nContent: $page\n\n";
62
63 0 ▶ while ($page =~ /href *?= *?("|')(.*?)('|")/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page =~ /href *?= *?("|')(.*?)('|")/cg) 0 0 0
64 0 my $link = URI->new($2)->abs($uri)->canonical;
65 0 ▶ if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) 0 0 0
Condition0%0/3
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
!ll&&!rl&&r
000
MC/DC0%0/2
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
$link->scheme =~ /^http/ $link->authority eq $uri->authority
Truth table0%0/3
A and B
A= $link->scheme =~ /^http/B= $link->authority eq $uri->authority
ABresult
0X0
100
111
66 0 my $address = $link->as_string;
67 0 ▶ while ($address =~ s/(\/|#)$//) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($address =~ s[(/|#)$][]) 0 0 0
68 0 ▶ $links{$address}++ unless $link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/ or $address =~ /#/;
Branch: 1 branch
BranchTrueFalseTotal
unless $link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/ or $address =~ /#/ 0 0 0
Condition0%0/3
$link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/ or $address =~ /#/
l!l&&r!l&&!r
000
MC/DC0%0/2
$link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/ or $address =~ /#/
$link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/ $address =~ /#/
Truth table0%0/3
A or B
A= $link->path =~ /\.(pdf|css|png|jpg|svg|webmanifest)/B= $address =~ /#/
ABresult
1X1
011
000
69 }
70 }
71 # Find forms
72 0 pos($page) = 0;
73 0 ▶ while ($page =~ /<form .*?action *?= *?("|')(.*?) *('|")/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page =~ /<form .*?action *?= *?("|')(.*?) *('|")/cg) 0 0 0
74 0 my $link = URI->new($2)->abs($uri)->canonical;
75 0 ▶ if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) 0 0 0
Condition0%0/3
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
!ll&&!rl&&r
000
MC/DC0%0/2
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
$link->scheme =~ /^http/ $link->authority eq $uri->authority
Truth table0%0/3
A and B
A= $link->scheme =~ /^http/B= $link->authority eq $uri->authority
ABresult
0X0
100
111
76 0 my $address = $link->as_string;
77 0 $links{$address}++ ;
78 }
79 }
80 # Find external JS files
81 0 pos($page) = 0;
82 0 ▶ while ($page =~ /<script .*?src *?= *?("|')(.*?) *('|")/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page =~ /<script .*?src *?= *?("|')(.*?) *('|")/cg) 0 0 0
83 0 my $link = URI->new($2)->abs($uri)->canonical;
84 0 ▶ if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) 0 0 0
Condition0%0/3
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
!ll&&!rl&&r
000
MC/DC0%0/2
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
$link->scheme =~ /^http/ $link->authority eq $uri->authority
Truth table0%0/3
A and B
A= $link->scheme =~ /^http/B= $link->authority eq $uri->authority
ABresult
0X0
100
111
85 0 my $address = $link->as_string;
86 0 my $timestamp = $self->{'timestamp'};
87 0 ▶ $timestamp = '' unless $timestamp;
Branch: 1 branch
BranchTrueFalseTotal
unless $timestamp 0 0 0
88 0 $address =~ s/(\?|\&)$timestamp=.*(\?|\&|$)//;
89 0 $links{$address}++;
90 }
91 }
92 # Find JS window.open links
93 0 pos($page) = 0;
94 0 ▶ while ($page =~ /(window|document).open\( *("|')(.*?)('|")/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($page =~ /(window|document).open\( *("|')(.*?)('|")/cg) 0 0 0
95 0 my $link = URI->new($3)->abs($uri)->canonical;
96 0 ▶ if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) {
Branch: 1 branch
BranchTrueFalseTotal
if ($link->scheme =~ /^http/ and $link->authority eq $uri->authority) 0 0 0
Condition0%0/3
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
!ll&&!rl&&r
000
MC/DC0%0/2
$link->scheme =~ /^http/ and $link->authority eq $uri->authority
$link->scheme =~ /^http/ $link->authority eq $uri->authority
Truth table0%0/3
A and B
A= $link->scheme =~ /^http/B= $link->authority eq $uri->authority
ABresult
0X0
100
111
97 0 my $address = $link->as_string;
98 0 ▶ while ($address =~ s/(\/|#)$//) {}
Branch: 1 branch
BranchTrueFalseTotal
if ($address =~ s[(/|#)$][]) 0 0 0
99 0 ▶ $links{$address}++ unless $link->path =~ /\.(pdf|css|png|jpg|svg)/;
Branch: 1 branch
BranchTrueFalseTotal
unless $link->path =~ /\.(pdf|css|png|jpg|svg)/ 0 0 0
100 }
101 }
102
103 0 ▶ &$callback($url, $page) if $callback;
Branch: 1 branch
BranchTrueFalseTotal
if $callback 0 0 0
104 0 ▶ $flag = 0 if $self->{'nolinks'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nolinks"} 0 0 0
105 }
106
107 0 ▶ return keys %links if $self->{'nolinks'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"nolinks"} 0 0 0
108 0 return keys %parsed;
109 }
110
111 0 ▶sub _fetch_page {
Subroutine _fetch_page: not called CC=5 SCAR=34.0
112 0 my ($self, $url) = @_;
113
114 0 my $resp = $self->{'http'}->request('GET', $url);
115 0 ▶ if ($resp->{'success'} && $resp->{'headers'}{'content-encoding'} && $resp->{'headers'}{'content-encoding'} =~ /gzip/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($resp->{"success"} and $resp->{"headers"}{"content-encoding"} and $resp->{"headers"}{"content-encoding"} =~ /gzip/i) 0 0 0
Condition0%0/6
$resp->{"success"} and $resp->{"headers"}{"content-encoding"} and $resp->{"headers"}{"content-encoding"} =~ /gzip/i
!ll&&!rl&&r
000
$resp->{"success"} and $resp->{"headers"}{"content-encoding"}
!ll&&!rl&&r
000
MC/DC0%0/3
$resp->{"success"} and $resp->{"headers"}{"content-encoding"} and $resp->{"headers"}{"content-encoding"} =~ /gzip/i
$resp->{"success"} $resp->{"headers"}{"content-encoding"} $resp->{"headers"}{"content-encoding"} =~ /gzip/i
Truth table0%0/4
A and B and C
A= $resp->{"success"}B= $resp->{"headers"}{"content-encoding"}C= $resp->{"headers"}{"content-encoding"} =~ /gzip/i
ABCresult
0XX0
10X0
1100
1111
116 0 my $content = $resp->{'content'};
117 0 my $uncompressed;
118 0 ▶ if (gunzip \$content => \$uncompressed) {
Branch: 1 branch
BranchTrueFalseTotal
if (gunzip(\$content, \$uncompressed)) { } 0 0 0
119 0 $resp->{'content'} = $uncompressed;
120 } else {
121 0 warn "Gunzip failed: $GunzipError\n";
122 }
123 }
124 0 return $resp;
125 }
126
127 3 1;
128
129
130 =head1 NAME
131
132 WWW::Crawl - A simple web crawler for extracting links and more from web pages
133
134 =head1 VERSION
135
136 This documentation refers to WWW::Crawl version 0.5.
137
138 =head1 SYNOPSIS
139
140 use WWW::Crawl;
141
142 my $crawler = WWW::Crawl->new();
143
144 my $url = 'https://example.com';
145
146 my @visited = $crawler->crawl($url, \&process_page);
147
148 sub process_page {
149 my $url = shift;
150 print "Visited: $url\n";
151 # Your processing logic here
152 }
153
154 =head1 DESCRIPTION
155
156 The C<WWW::Crawl> module provides a simple web crawling utility for extracting links and other resources from web pages within a single domain. It can be used to recursively explore a website and retrieve URLs, including those found in HTML href attributes, form actions, external JavaScript files, and JavaScript window.open links.
157
158 C<WWW::Crawl> will not stray outside the supplied domain.
159
160 =head1 CONSTRUCTOR
161
162 =head2 new(%options)
163
164 Creates a new C<WWW::Crawl> object. You can optionally provide the following options as key-value pairs:
165
166 =over 4
167
168 =item *
169
170 C<agent>: The user agent string to use for HTTP requests. Defaults to "Perl-WWW-Crawl-VERSION" where VERSION is the module version.
171
172 C<timestamp>: If a timestamp is added to external JavaScript files to ensure the latest version is loaded by the browser, this option prevents multiple copied of the same file being indexed by ignoring the timestamp query parameter.
173
174 C<nolinks>: Don't follow links found in the starting page. This option is provided for testing and prevents C<WWW::Crawl> following the links it finds. It also affects the return value of the L<crawl|WWW::Crawl#crawl($url,-[$callback])> method.
175
176 =back
177
178 =head1 METHODS
179
180 =head2 crawl($url, [$callback])
181
182 Starts crawling the web starting from the given URL. The C<$url> parameter specifies the starting URL.
183
184 The optional C<$callback> parameter is a reference to a subroutine that will be called for each visited page. It receives the URL of the visited page and the body of the page as arguments.
185
186 The C<crawl> method will explore the provided URL and its linked resources. It will also follow links found in form actions, external JavaScript files, and JavaScript window.open links. The crawling process continues until no more unvisited links are found.
187
188 In exploring the website, C<crawl> will ignore links to the following types of file C<.pdf>, C<.css>, C<.png>, C<.jpg>, C<.svg> and C<.webmanifest>
189
190 Returns an array of URLs that were parsed during the crawl. Unless the C<nolinks> option is passed to L<new|WWW::Crawl#new(%options)>, then it returns an array of links found on the intial page.
191
192 =head1 AUTHOR
193
194 Ian Boddison, C<< <bod at cpan.org> >>
195
196 =head1 BUGS
197
198 Please report any bugs or feature requests to C<bug-www-crawl at rt.cpan.org>, or through
199 the web interface at L<https://rt.cpan.org/NoAuth/ReportBug.html?Queue=WWW-Crawl>. I will be notified, and then you'll
200 automatically be notified of progress on your bug as I make changes.
201
202 =head1 SUPPORT
203
204 You can find documentation for this module with the perldoc command.
205
206 perldoc WWW::Crawl
207
208
209 You can also look for information at:
210
211 =over 4
212
213 =item * GitHub
214
215 L<https://github.com/IanBod/WWW-Crawl>
216
217 =item * RT: CPAN's request tracker (report bugs here)
218
219 L<https://rt.cpan.org/NoAuth/Bugs.html?Dist=WWW-Crawl>
220
221 =item * Search CPAN
222
223 L<https://metacpan.org/release/WWW-Crawl>
224
225 =back
226
227
228 =head1 ACKNOWLEDGEMENTS
229
230
231 =head1 LICENSE AND COPYRIGHT
232
233 This software is Copyright (c) 2023-2026 by Ian Boddison.
234
235 This program is released under the following license:
236
237 Perl
238
239
240 =cut
241
242 3 1; # End of WWW::Crawl
243
244
↑ Summary Crawl/Auto.pm »