line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::DOM; |
2
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
26
|
|
3
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
27
|
|
4
|
1
|
|
|
1
|
|
4
|
use base 'Object'; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
54
|
|
5
|
1
|
|
|
1
|
|
5
|
use Array; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
16
|
|
6
|
1
|
|
|
1
|
|
1154
|
use HTML::TreeBuilder::XPath; |
|
1
|
|
|
|
|
114902
|
|
|
1
|
|
|
|
|
15
|
|
7
|
1
|
|
|
1
|
|
1244
|
use HTML::Selector::XPath qw(selector_to_xpath); |
|
1
|
|
|
|
|
5255
|
|
|
1
|
|
|
|
|
3805
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
=head1 DESCRIPTION |
10
|
|
|
|
|
|
|
A mini implement of Web::Query, but all method return Array object, no matter wantarray or no. |
11
|
|
|
|
|
|
|
I only want something work with web_get, so modify-relate methods all deleted. |
12
|
|
|
|
|
|
|
=cut |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub new { |
15
|
0
|
|
|
0
|
1
|
|
my ( $pkg, $html ) = @_; |
16
|
0
|
|
|
|
|
|
my $tree = HTML::TreeBuilder::XPath->new; |
17
|
0
|
|
|
|
|
|
$tree->ignore_unknown(0); |
18
|
0
|
|
|
|
|
|
$tree->store_comments(1); |
19
|
0
|
|
|
|
|
|
$tree->parse_content($html); |
20
|
0
|
|
|
|
|
|
my $self = $pkg->new_from_element( Array->new( $tree->guts ) ); |
21
|
0
|
|
|
|
|
|
$self->{need_delete}++; |
22
|
0
|
|
|
|
|
|
return $self; |
23
|
|
|
|
|
|
|
} |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub new_from_element { |
26
|
0
|
|
|
0
|
0
|
|
my $class = shift; |
27
|
0
|
0
|
|
|
|
|
my $trees = ref $_[0] eq 'Array' ? $_[0] : Array->new( $_[0] ); |
28
|
0
|
|
0
|
|
|
|
return bless { trees => $trees, before => $_[1] }, ref($class) || $class; |
29
|
|
|
|
|
|
|
} |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
sub end { |
32
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
33
|
0
|
|
|
|
|
|
return $self->{before}; |
34
|
|
|
|
|
|
|
} |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub size { |
37
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
38
|
0
|
|
|
|
|
|
return $self->{trees}->size; |
39
|
|
|
|
|
|
|
} |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
sub parent { |
42
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
43
|
0
|
|
|
|
|
|
my $new = Array->new; |
44
|
|
|
|
|
|
|
$self->{trees}->each( |
45
|
|
|
|
|
|
|
sub { |
46
|
0
|
|
|
0
|
|
|
$new->push( shift->getParentNode() ); |
47
|
|
|
|
|
|
|
} |
48
|
0
|
|
|
|
|
|
); |
49
|
0
|
|
0
|
|
|
|
return ( ref $self || $self )->new_from_element( $new, $self ); |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub first { |
53
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
54
|
0
|
|
0
|
|
|
|
return ( ref $self || $self ) |
|
|
|
0
|
|
|
|
|
55
|
|
|
|
|
|
|
->new_from_element( Array->new( $self->{trees}->get(0) || () ), $self ); |
56
|
|
|
|
|
|
|
} |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub last { |
59
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
60
|
0
|
|
0
|
|
|
|
return ( ref $self || $self ) |
|
|
|
0
|
|
|
|
|
61
|
|
|
|
|
|
|
->new_from_element( Array->new( $self->{trees}->get(-1) || () ), $self ); |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=head2 find |
65
|
|
|
|
|
|
|
my $dom = HTTP::DOM->new($content); |
66
|
|
|
|
|
|
|
print $dom->find('#m')->text->get(0); |
67
|
|
|
|
|
|
|
=cut |
68
|
|
|
|
|
|
|
sub find { |
69
|
0
|
|
|
0
|
1
|
|
my ( $self, $selector ) = @_; |
70
|
0
|
|
|
|
|
|
my $xpath_rootless = selector_to_xpath($selector); |
71
|
|
|
|
|
|
|
|
72
|
0
|
|
|
|
|
|
my $new = Array->new; |
73
|
|
|
|
|
|
|
$self->{trees}->each( |
74
|
|
|
|
|
|
|
sub { |
75
|
0
|
|
|
0
|
|
|
my $tree = shift; |
76
|
0
|
0
|
0
|
|
|
|
$new->push($tree) |
77
|
|
|
|
|
|
|
if defined $tree->parent && $tree->matches($xpath_rootless); |
78
|
0
|
0
|
|
|
|
|
$new->push( |
79
|
|
|
|
|
|
|
$tree->findnodes( |
80
|
|
|
|
|
|
|
selector_to_xpath( |
81
|
|
|
|
|
|
|
$selector, root => defined $tree->parent ? './' : '/' |
82
|
|
|
|
|
|
|
) |
83
|
|
|
|
|
|
|
) |
84
|
|
|
|
|
|
|
); |
85
|
|
|
|
|
|
|
} |
86
|
0
|
|
|
|
|
|
); |
87
|
|
|
|
|
|
|
|
88
|
0
|
|
0
|
|
|
|
return ( ref $self || $self )->new_from_element( $new, $self ); |
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
|
|
|
|
|
|
=head2 html |
92
|
|
|
|
|
|
|
=cut |
93
|
|
|
|
|
|
|
sub html { |
94
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
95
|
0
|
|
|
|
|
|
my $html = Array->new; |
96
|
|
|
|
|
|
|
$self->{trees}->each( |
97
|
|
|
|
|
|
|
sub { |
98
|
0
|
|
|
0
|
|
|
$html->push( shift->as_HTML ); |
99
|
|
|
|
|
|
|
} |
100
|
0
|
|
|
|
|
|
); |
101
|
0
|
|
|
|
|
|
return $html->join("\n"); |
102
|
|
|
|
|
|
|
} |
103
|
|
|
|
|
|
|
|
104
|
|
|
|
|
|
|
sub xml { |
105
|
0
|
|
|
0
|
0
|
|
my $self = shift; |
106
|
0
|
|
|
|
|
|
my $html = Array->new; |
107
|
|
|
|
|
|
|
$self->{trees}->each( |
108
|
|
|
|
|
|
|
sub { |
109
|
0
|
|
|
0
|
|
|
$html->push( shift->as_XML_indented ); |
110
|
|
|
|
|
|
|
} |
111
|
0
|
|
|
|
|
|
); |
112
|
0
|
|
|
|
|
|
return $html->join("\n"); |
113
|
|
|
|
|
|
|
} |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=head2 text |
116
|
|
|
|
|
|
|
my $dom = HTTP::DOM->new($content); |
117
|
|
|
|
|
|
|
print $dom->text->get(0); |
118
|
|
|
|
|
|
|
=cut |
119
|
|
|
|
|
|
|
sub text { |
120
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
121
|
0
|
|
|
|
|
|
my $text = Array->new; |
122
|
|
|
|
|
|
|
$self->{trees}->each( |
123
|
|
|
|
|
|
|
sub { |
124
|
0
|
|
|
0
|
|
|
$text->push( shift->as_text ); |
125
|
|
|
|
|
|
|
} |
126
|
0
|
|
|
|
|
|
); |
127
|
0
|
|
|
|
|
|
return $text->join("\n"); |
128
|
|
|
|
|
|
|
} |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
=head2 attr |
131
|
|
|
|
|
|
|
my $url = 'http://www.baidu.com/'; |
132
|
|
|
|
|
|
|
my $hc = HTTP::Client->new; |
133
|
|
|
|
|
|
|
my $content = $hc->web_get($url); |
134
|
|
|
|
|
|
|
my $dom = HTTP::DOM->new($content); |
135
|
|
|
|
|
|
|
print $dom->find('meta')->attr('content')->get(0); |
136
|
|
|
|
|
|
|
=cut |
137
|
|
|
|
|
|
|
sub attr { |
138
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
139
|
0
|
|
|
|
|
|
my @attr_keys = @_; |
140
|
0
|
|
|
|
|
|
my $retval = Array->new; |
141
|
|
|
|
|
|
|
$self->{trees}->each( |
142
|
|
|
|
|
|
|
sub { |
143
|
0
|
|
|
0
|
|
|
$retval->push( shift->attr(@attr_keys) ); |
144
|
|
|
|
|
|
|
} |
145
|
0
|
|
|
|
|
|
); |
146
|
0
|
|
|
|
|
|
return $retval->join("\n"); |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
sub DESTROY { |
150
|
0
|
0
|
|
0
|
|
|
if ( $_[0]->{need_delete} ) { |
151
|
0
|
|
|
|
|
|
$_->delete for @{ $_[0]->{trees} }; |
|
0
|
|
|
|
|
|
|
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
1; |