HTML Tag Counter

S

Shah Karim

Hi,

I am looking for a simple script that will count the tags in an HTML
document, and tally up the total number of occurences of each tag. The
end result should be a list of all tags found and the number of times
each tag was encountered.

Does anyone know if such a program is available freely somewhere?
Pointers would be appreciated.

Thanks,
Shah
 
T

Tassilo v. Parseval

Also sprach Shah Karim:
I am looking for a simple script that will count the tags in an HTML
document, and tally up the total number of occurences of each tag. The
end result should be a list of all tags found and the number of times
each tag was encountered.

Does anyone know if such a program is available freely somewhere?
Pointers would be appreciated.

See <[email protected]>. It includes an example that
counts the opening and closing tags. It should be pretty easy to change
this snippet to use a hash to store the tags and increase their number
each time they show up in the document.

Tassilo
 
G

Gisle Aas

I am looking for a simple script that will count the tags in an HTML
document, and tally up the total number of occurences of each tag. The
end result should be a list of all tags found and the number of times
each tag was encountered.

Does anyone know if such a program is available freely somewhere?

This is such a program.

------------------------------------------------------------->%-----
#!/usr/bin/perl -w

use strict;
use HTML::parser;

my %count;
my $p = HTML::parser->new(start_h => [sub { $count{$_[0]}++ }, "tagname"],
end_h => [sub { $count{"/$_[0]"}++ }, "tagname"],
);
$p->parse_file(*STDIN);

for (sort { $count{$b} <=> $count{$a} } keys %count) {
printf "%5d %s\n", $count{$_}, $_;
}
 
G

GreenLight

Gisle Aas said:
#!/usr/bin/perl -w

use strict;
use HTML::parser;

my %count;
my $p = HTML::parser->new(start_h => [sub { $count{$_[0]}++ }, "tagname"],
end_h => [sub { $count{"/$_[0]"}++ }, "tagname"],
);
$p->parse_file(*STDIN);

for (sort { $count{$b} <=> $count{$a} } keys %count) {
printf "%5d %s\n", $count{$_}, $_;
}

Here is a variation that print the list in alphabetical order, with
opening and closing tags (if applicable) listed together:

#!/usr/bin/perl -w

use strict;
use HTML::parser;

my %count;
my $p = HTML::parser->new(start_h => [sub { $count{$_[0]}++ },
"tagname"],
end_h => [sub { $count{"/$_[0]"}++ }, "tagname"],
);
$p->parse_file(<THIS IS WHERE YOU DECIDE WHAT FILE TO PARSE>);

for (sort keys %count) {
next if(/^\//);
printf "%5d %s\n", $count{$_}, $_;
my $slashed = "/" . $_;
if (defined($count{$slashed})) {
printf "%5d %s\n", $count{$slashed}, $slashed;
}
}
 
S

Shah Karim

Thanks!

I will try this out.

Gisle Aas said:
#!/usr/bin/perl -w

use strict;
use HTML::parser;

my %count;
my $p = HTML::parser->new(start_h => [sub { $count{$_[0]}++ }, "tagname"],
end_h => [sub { $count{"/$_[0]"}++ }, "tagname"],
);
$p->parse_file(*STDIN);

for (sort { $count{$b} <=> $count{$a} } keys %count) {
printf "%5d %s\n", $count{$_}, $_;
}

Here is a variation that print the list in alphabetical order, with
opening and closing tags (if applicable) listed together:

#!/usr/bin/perl -w

use strict;
use HTML::parser;

my %count;
my $p = HTML::parser->new(start_h => [sub { $count{$_[0]}++ },
"tagname"],
end_h => [sub { $count{"/$_[0]"}++ }, "tagname"],
);
$p->parse_file(<THIS IS WHERE YOU DECIDE WHAT FILE TO PARSE>);

for (sort keys %count) {
next if(/^\//);
printf "%5d %s\n", $count{$_}, $_;
my $slashed = "/" . $_;
if (defined($count{$slashed})) {
printf "%5d %s\n", $count{$slashed}, $slashed;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,147
Messages
2,570,835
Members
47,383
Latest member
EzraGiffor

Latest Threads

Top