Thread html parsen (7 answers)
Opened by Froschpopo at 2008-07-22 10:01

Froschpopo
 2008-07-22 20:42
#112449 #112449
User since
2003-08-15
2653 Artikel
BenutzerIn
[default_avatar]
renee+2008-07-22 09:21:59--
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
38
39
40
#!/usr/bin/perl

use strict;
use warnings;
use HTML::Parser;

my @found = ();
my $p = HTML::Parser->new(
        start_h => [\&start, 'self,tagname,attr'],
        end_h   => [\&end, 'self,tagname'],
        text_h  => [\&text, 'self,dtext']
);

chdir("c:/users/frosch/documents/schulen/");

$p->parse_file("bw.html");


sub start {
        my ($self, $tagname, $attr) = @_;
        $self->{'div'} = 1 if $tagname eq 'div';
        $self->{parse_div} = 1 if $tagname eq 'div' and $attr->{class} eq 'p';
}

sub end {
        my ($self, $tagname) = @_;
        $self->{parse_div} = 0 if $tagname eq 'div';
}

sub text {
        my ($self, $dtext) = @_;
        $dtext =~ s/\n\r//;
        push @found, $dtext if $self->{parse_div};
}

for (@found) {
        print $_,"\n";
}

print $#found," Ergebnisse.\n";


Im Wiki stehen aber auch Beispiele wie man an Attribute kommt...

Wenn es nicht so sehr auf Geschwindigkeit ankommt, würde ich eher CPAN:Web::Scraper verwenden.

hm, also irgendwie verursacht Zeile 22 einen "Use of uninitialized value in string eq..."

View full thread html parsen