find("p")) { pr" /> find("p")) { pr" /> find("p")) { pr"/>

HTML parser by perl script

498 Views Asked by At
#!/usr/bin/perl

use HTML::TreeBuilder;

my $tree = HTML::TreeBuilder->new; 

$tree->parse_file("sample.html");

foreach my $anchor ($tree->find("p")) {

  print $anchor->as_text, "\n";

}

my code is not printing any output. $tree->find("p") is returning NULL.

1

There are 1 best solutions below

0
Richard Huxton On

You're not opening the file, or it's completely unparseable.

Try something like:

my $file = shift(@ARGV) or die "No filename given";
$tree->parse_file($file) or die "Unable to open $file";

That way you can check which it is.

Your script worked fine when I had the following for sample.html

<html>
<head><title>test file</title></head>
<body>
    <h1>Title</h1>
    <p>First para</p>
    <p>Second para</p>
    <div>
        <P>Third para</P>
    </div>
</body>
</html>