You are calling HTML::TreeBuilder->new_from_content
yet you are supplying a URL instead of content. You have to get
the HTML before you can pass it to HTML::TreeBuilder
.
Perhaps the simplest way is to use LWP::Simple which imports a subroutine called get
. This will read the data at the URL and return it as a string.
The reason your conditional block is never executed is that you have a space in the tag name. You need "div"
instead of "div "
.
Also note the following:
You shouldn't output a single string by using printf
with that string as a format specifier. It may generate missing argument warnings and fail to output the string properly.
You should ideally use lexical file handles and the three-argument form of open
. You should also check the status of all open
calls and respond accordingly.
Your scalar variable $div
is a blessed hash reference, so printing it as it is will output something like HTML::Element=HASH(0xfffffff)
. You need to call its methods to extract the values you want to display
With these errors corrected your code looks like this, although I haven't formatted the output as I can't tell what you want.
use strict;
use warnings;
use HTML::TreeBuilder;
use LWP::Simple;
my $url = "http://prospectus.ulster.ac.uk/modules/index/index/selCampus/JN/selProgramme/2132/hModuleCode/COM137";
my $html = get $url;
my $tree = HTML::TreeBuilder->new_from_content($html);
if (my $div = $tree->look_down(_tag => "div", class => "col col60 moduledetail")) {
print $div->as_text(), "\n";
open my $fh, '>', 'mytest.txt' or die "Unable to open output file: $!";
print $fh $div->as_text, "\n";
}
print $tree->look_down(_tag => "th", class => "moduleCode")->as_text, "\n";