line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTML::Extract; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
37007
|
use 5.008006; |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
38
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
15996
|
use utf8; |
|
1
|
|
|
|
|
26
|
|
|
1
|
|
|
|
|
9
|
|
6
|
1
|
|
|
1
|
|
44
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
7
|
1
|
|
|
1
|
|
3791
|
use HTML::TreeBuilder; |
|
1
|
|
|
|
|
85528
|
|
|
1
|
|
|
|
|
22
|
|
8
|
1
|
|
|
1
|
|
54
|
use HTML::Element; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
5
|
|
9
|
1
|
|
|
1
|
|
1512
|
use LWP::UserAgent; |
|
1
|
|
|
|
|
50944
|
|
|
1
|
|
|
|
|
36
|
|
10
|
1
|
|
|
1
|
|
11
|
use HTML::Parser; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
22
|
|
11
|
1
|
|
|
1
|
|
1121
|
use Encode; |
|
1
|
|
|
|
|
11306
|
|
|
1
|
|
|
|
|
1038
|
|
12
|
|
|
|
|
|
|
# use encoding 'utf8'; |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
require Exporter; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export |
19
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead. |
20
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants. |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
# This allows declaration use HTML::Extract ':all'; |
23
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK |
24
|
|
|
|
|
|
|
# will save memory. |
25
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw( |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
) ] ); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
our @EXPORT = qw( |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
); |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
our $VERSION = '0.25'; |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
# Preloaded methods go here. |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub new { |
42
|
0
|
|
|
0
|
0
|
|
my $package = shift; |
43
|
0
|
|
|
|
|
|
my $self= { |
44
|
|
|
|
|
|
|
_uri=> undef, |
45
|
|
|
|
|
|
|
_raw=> undef, |
46
|
|
|
|
|
|
|
_remnant=> undef, |
47
|
|
|
|
|
|
|
_tagclass=> undef, |
48
|
|
|
|
|
|
|
_atagname=> undef, |
49
|
|
|
|
|
|
|
_tagid=> undef, |
50
|
|
|
|
|
|
|
}; |
51
|
|
|
|
|
|
|
#return bless({}, $package); |
52
|
0
|
|
|
|
|
|
return bless ($self,$package); |
53
|
|
|
|
|
|
|
} |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
|
56
|
|
|
|
|
|
|
sub settagclass { |
57
|
0
|
|
|
0
|
0
|
|
my ( $self, $tagclass ) = @_; |
58
|
0
|
0
|
|
|
|
|
$self->{_tagclass} = $tagclass if defined($tagclass); |
59
|
0
|
|
|
|
|
|
return $self->{_tagclass}; |
60
|
|
|
|
|
|
|
} |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
sub settagname { |
63
|
0
|
|
|
0
|
0
|
|
my ( $self, $tagname ) = @_; |
64
|
0
|
0
|
|
|
|
|
$self->{_atagname} = $tagname if defined($tagname); |
65
|
0
|
|
|
|
|
|
return $self->{_atagname}; |
66
|
|
|
|
|
|
|
} |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
sub settagid { |
69
|
0
|
|
|
0
|
0
|
|
my ( $self, $tagid ) = @_; |
70
|
0
|
0
|
|
|
|
|
$self->{_tagid} = $tagid if defined($tagid); |
71
|
0
|
|
|
|
|
|
return $self->{_tagid}; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub seturi { |
75
|
0
|
|
|
0
|
0
|
|
my ( $self, $uri ) = @_; |
76
|
0
|
0
|
|
|
|
|
$self->{_uri} = $uri if defined($uri); |
77
|
0
|
|
|
|
|
|
return $self->{_uri}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
sub gethtml { |
81
|
|
|
|
|
|
|
#my ( $self, $uri, $tagclass, $tagname, $tagid) = @_; |
82
|
0
|
|
|
0
|
0
|
|
my ( $self, $uri, $command, $areturntype) = @_; |
83
|
0
|
|
|
|
|
|
my $commandname; |
84
|
|
|
|
|
|
|
my $commandvalue; |
85
|
|
|
|
|
|
|
|
86
|
0
|
0
|
|
|
|
|
$areturntype=~/\=(.*)$/ if defined($areturntype); |
87
|
0
|
0
|
|
|
|
|
if($1){ |
88
|
0
|
|
|
|
|
|
$areturntype=$1; |
89
|
|
|
|
|
|
|
} |
90
|
0
|
|
|
|
|
|
my $toreturn="HTML"; |
91
|
0
|
0
|
|
|
|
|
$toreturn=$areturntype if defined($areturntype); |
92
|
|
|
|
|
|
|
|
93
|
0
|
0
|
|
|
|
|
if(!$command eq ""){ |
94
|
0
|
|
|
|
|
|
($commandname,$commandvalue)=split(/=/,$command); |
95
|
|
|
|
|
|
|
} else { |
96
|
0
|
|
|
|
|
|
$commandname="tagname"; |
97
|
0
|
|
|
|
|
|
$commandvalue="html"; |
98
|
|
|
|
|
|
|
} |
99
|
0
|
|
|
|
|
|
my $tagclass; |
100
|
|
|
|
|
|
|
my $tagname; |
101
|
0
|
|
|
|
|
|
my $tagid; |
102
|
0
|
0
|
|
|
|
|
if($commandname eq "tagclass"){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
$tagclass=$commandvalue; |
104
|
|
|
|
|
|
|
} elsif ($commandname eq "tagname") { |
105
|
0
|
|
|
|
|
|
$tagname=$commandvalue; |
106
|
|
|
|
|
|
|
} elsif($commandname eq "tagid"){ |
107
|
0
|
|
|
|
|
|
$tagid=$commandvalue; |
108
|
|
|
|
|
|
|
} |
109
|
|
|
|
|
|
|
|
110
|
0
|
0
|
|
|
|
|
$self->seturi($uri) if defined($uri); |
111
|
0
|
0
|
|
|
|
|
$self->settagclass($tagclass) if defined($tagclass); |
112
|
0
|
0
|
|
|
|
|
$self->settagname($tagname) if defined($tagname); |
113
|
0
|
0
|
|
|
|
|
$self->settagid($tagid) if defined($tagid); |
114
|
|
|
|
|
|
|
|
115
|
0
|
|
|
|
|
|
my $browser=LWP::UserAgent->new( |
116
|
|
|
|
|
|
|
'Accept-Charset' => 'utf-8', |
117
|
|
|
|
|
|
|
); |
118
|
|
|
|
|
|
|
# my $tf=HTML::TagFilter->new(allow=>{}); |
119
|
0
|
|
|
|
|
|
my $tree = HTML::TreeBuilder->new(); |
120
|
0
|
|
|
|
|
|
my $content = $browser->get($uri); |
121
|
0
|
0
|
|
|
|
|
return "Couldn't get $uri\n" unless defined $content; |
122
|
|
|
|
|
|
|
# Problem; the system does not know that content has UTF8 flavour |
123
|
|
|
|
|
|
|
# so tell it that it does... |
124
|
0
|
|
|
|
|
|
my $content2 = $content->content; |
125
|
0
|
|
|
|
|
|
Encode::_utf8_on($content2); |
126
|
|
|
|
|
|
|
# $tree->parse($content->content)|| die "Bah! $!\n"; |
127
|
0
|
0
|
|
|
|
|
$tree->parse($content2)|| die "Bah! $!\n"; |
128
|
0
|
|
|
|
|
|
$tree->eof(); |
129
|
0
|
|
|
|
|
|
my @candidates; |
130
|
|
|
|
|
|
|
|
131
|
0
|
0
|
|
|
|
|
if($tagclass){ |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
@candidates = $tree->look_down ("class", qr/$tagclass/); |
133
|
|
|
|
|
|
|
} elsif ($tagname){ |
134
|
0
|
|
|
|
|
|
@candidates = $tree->look_down("_tag",qr/$tagname/); |
135
|
|
|
|
|
|
|
} elsif ($tagid){ |
136
|
0
|
|
|
|
|
|
@candidates = $tree->look_down("id",qr/$tagid/); |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
} |
139
|
0
|
0
|
|
|
|
|
if($#candidates>-1){ |
140
|
0
|
0
|
0
|
|
|
|
if($toreturn eq "text" || $toreturn eq "txt"){ |
141
|
0
|
|
|
|
|
|
return $candidates[0]->as_text(); |
142
|
|
|
|
|
|
|
} else { |
143
|
0
|
|
|
|
|
|
my $text=$candidates[0]->as_HTML(); |
144
|
0
|
|
|
|
|
|
return $text; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
} else{ |
147
|
0
|
|
|
|
|
|
return "No candidates found"; |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
1; |
154
|
|
|
|
|
|
|
__END__ |