Daniel Thoma: Daten aus einer HTML-Seite auslesen

Beitrag lesen

Hallo Michael,

Das ist die perfekte Lösung.
Das Programm parsed eine HTML-Datei mit Hilfe von HTML::TokeParser und gibt dann informationen
über deren Aufbau aus.

use strict;
require HTML::TokeParser;

my $file = "parse.html";
my $p = HTML::TokeParser->new($file);
while(my $token = $p->get_token())
{
if($token->[0] eq "S")
{
  start($token->[1],$token->[2],$token->[3],$token->[4]);
}
elsif($token->[0] eq "E")
{
  end($token->[1],$token->[2]);
}
elsif($token->[0] eq "T")
{
  text($token->[1]);
}
elsif($token->[0] eq "C")
{
  comment($token->[1]);
}
elsif($token->[0] eq "D")
{
  declaration($token->[1]);
}
}

sub declaration
{
my $decl = $_[0];
print("Decl: $decl\n");
}

sub start
{
my $tag = $_[0];
my  %attr = %{$_[1]};
my  $attrseq = $_[2];
my  $origtext = $_[3];
print("STag: $tag (\n");
print(map {"$_ => $attr{$_}\n"} keys %attr);
print(")\n");
}

sub end
{
my $tag = $_[0];
print("ETag: $tag\n");
}

sub text
{
my $text = $_[0];
print("Text: {\n$text\n}\n");
}

sub comment
{
my $comment = $_[0];
print("Comment: {\n$comment\n}\n");
}

Die Fuktionen decleration, start, end, text und comment muß man nur noch nach belieben abändern.
Das sollte bei allen HTML-Files fuktionieren.

Tschüs

Daniel