Leser: 27
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<html>
<body>
<head>
<TITLE>
Testseite
</TITLE>
</head>
<table width="400" style="border-collapse: collapse" bordercolor="#B3B3C1" cellpadding="0" border="1" cellspacing="0">
<tr>
<td height=25 valign=middle bgcolor="#336699"> <font color="#FFFFFF">Aktivität </font> </td>
<td height=25 valign=middle bgcolor="#F4F4F4"> <b>Doktor</b></td>
</tr>
<tr>
<td height=25 valign=middle bgcolor="#336699"> <font color="#FFFFFF">Land </font> </td>
<td height=25 valign=middle bgcolor="#F4F4F4"> <b>Deutschland </b> ...</td>
</tr>
</table>
</body></html>
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
#! /usr/bin/perl use strict; use warnings; use HTML::Parser; my @texts; my $string = qq~ <tr> <td height=25 valign=middle bgcolor="#336699"> <font color="#FFFFFF">Land </font> </td> <td height=25 valign=middle bgcolor="#F4F4F4"> <b>Deutschland </b> ...</td> </tr>~; my $p = HTML::Parser->new(); $p->handler(start => \&start_handler,"tagname,attr,self"); $p->parse($string); print $_,"\n" for @texts; sub start_handler{ return if(shift ne 'td'); my $self = shift; my $text; $self->handler(text => sub{$text = shift;},"dtext"); $self->handler(end => sub{push @texts,$text if shift eq 'td'},"tagname"); }
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
#!/usr/bin/perl
use strict;
use warnings;
while (<DATA>) {
if (m~<td\s+height[^>]+>(.+)</td>~) {
my $found = $1;
$found =~ s~ ~~g;
$found =~ s~</?font[^>]*>~~g;
$found =~ s~</?b>~~g;
$found =~ s~^\s+~~;
$found =~ s~\s+$~~;
print "gefunden: [$found]\n";
}
}
__DATA__
<html>
<body>
<head>
<TITLE>
Testseite
</TITLE>
</head>
<table width="400" style="border-collapse: collapse" bordercolor="#B3B3C1" cellpadding="0" border="1" cellspacing="0">
<tr>
<td height=25 valign=middle bgcolor="#336699"> <font color="#FFFFFF">Aktivität </font> </td>
<td height=25 valign=middle bgcolor="#F4F4F4"> <b>Doktor</b></td>
</tr>
<tr>
<td height=25 valign=middle bgcolor="#336699"> <font color="#FFFFFF">Land </font> </td>
<td height=25 valign=middle bgcolor="#F4F4F4"> <b>Deutschland </b> ...</td>
</tr>
</table>
</body></html>
1
2
3
4
5
C:\Daten\perl>perl td_finden.pl
gefunden: [Aktivitõt]
gefunden: [Doktor]
gefunden: [Land]
gefunden: [Deutschland ...]
2009-04-21T13:05:53 MisterL...(vorausgesetzt, sie funktionieren: "Can't call method "handler" on unblessed reference at ren_html.pl at line 23.")
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
#!/usr/bin/perl use warnings; use strict; use HTML::TreeBuilder::XPath; my $s = getHTMLString(); my $tree= HTML::TreeBuilder::XPath->new; $tree->parse($s); my @a = $tree->findnodes_as_strings ('//td'); for my $i (@a) { print "$i\n", } sub getHTMLString { return q(<html> <body> <head> <TITLE> Testseite </TITLE> </head> <table width="400" style="border-collapse: collapse" bordercolor="#B3B3C1" cellpadding="0" border="1" cellspacing="0"> <tr> <td height=25 valign=middle bgcolor="#336699"> <font color="#FFFFFF">Aktivität </font> </td> <td height=25 valign=middle bgcolor="#F4F4F4"> <b>Doktor</b></td> </tr> <tr> <td height=25 valign=middle bgcolor="#336699"> <font color="#FFFFFF">Land </font> </td> <td height=25 valign=middle bgcolor="#F4F4F4"> <b>Deutschland </b> ...</td> </tr> </table> </body></html>); }
2019-11-23T15:24:30 hlubenowAlso wirklich, das solltest du aber wissen, wer das is'.Aber was macht der hier?