Thread HTML Parser - Inhalte nicht immer vollständig (4 answers)
Opened by Gast at 2007-11-18 21:51

Gast Gast
 2007-11-18 21:51
#102541 #102541
Hallo,

ich möchte gerne den Inhalt einer html-Seite parsen, d.h. in meinem Fall den Inhalt zwischen den body-Tags unter Ausschluss von Inhalten wie Linkbezeichnungen, Formularinhalten etc. auslesen. Das klappt zum Teil auch ganz gut. Bei einigen Seiten jedoch erhalte ich in meinem Ausgabestring in der Variable $content nur einen Teil des Inhalts. Ein Beispiel dafür wäre Youtube.com. Hier bekomme ich die Beschreibungen der zwei ersten "featured videos" und danach ist Schluss, obwohl auf der Seite noch viel mehr steht. Hat jemand eine Ahnung woran es liegen könnte? Bei anderen Seiten dagegen bekomme ich wieder den kompletten Inhalt. Über Hilfe würde ich mich sehr freuen.

Code (perl): (dl )
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
my $parser = HTML::Parser->new(
start_h => [ \&_starttag, 'self, tagname, text, attr' ],
end_h => [ \&_endtag, 'self, tagname' ],
text_h => [ \&_text, 'self, text, dtext' ]
);

$parser->parse($response->content());

sub _starttag {
my ($self, $tag, $attr) = @_;
$self->{'_body'} = 1 if($tag eq 'body');
$self->{'_body'} = 0 if($tag eq 'script');
$self->{'_body'} = 0 if($tag eq 'a');
$self->{'_body'} = 0 if($tag eq 'label');
$self->{'_body'} = 0 if($tag eq 'option');
$self->{'_body'} = 0 if($tag eq 'form');
}

sub _endtag {
my ($self, $tag) = @_;
$self->{'_body' } = 0 if($tag eq 'body' );
$self->{'_body' } = 1 if($tag eq 'a' );
$self->{'_body'} = 1 if($tag eq 'script');
$self->{'_body'} = 1 if($tag eq 'label');
$self->{'_body'} = 1 if($tag eq 'option');
$self->{'_body'} = 1 if($tag eq 'form');
}

sub _text {
my ($self, $dtext) = @_;
$dtext =~ s/\A\s+//;
$dtext =~ s/\s+\z//;
return() unless ( length($dtext) > 0 and $dtext =~ /[^\s]/ );
if ($self->{'_body'} == 1) {
$content=$content.$dtext." ";
}
}

View full thread HTML Parser - Inhalte nicht immer vollständig