Skip to content Skip to sidebar Skip to footer

How Do I Extract An Html Title With Perl?

Is there a way to extract HTML page title using Perl? I know it can be passed as a hidden variable during form submit and then retrieved in Perl that way but I was wondering if th

Solution 1:

I would use pQuery. It works just like jQuery.

You can say:

use pQuery;
my $page = pQuery("http://google.com/");
my $title = $page->find('title');
say"The title is: ", $title->html;

Replacing stuff is similar:

$title->html('New Title');
say "The entirety of google.com with my new title is: ", $page->html;

You can pass an HTML string to the pQuery constructor, which it sounds like you want to do.

Finally, if you want to use arbitrary HTML as a "template", and then "refine" that with Perl commands, you want to use Template::Refine.

Solution 2:

HTML::HeadParser does this for you.

Solution 3:

It's not clear to me what you are asking. You seem to be talking about something that could run in the user's browser, or at least something that already has an html page loaded.

If that's not the case, the answer is URI::Title.

Solution 4:

use strict;
use LWP::Simple;

my $url = 'http://www.google.com'|| die"Specify URL on the cmd line";
my $html = get ($url);
$html =~ m{<TITLE>(.*?)</TITLE>}gism;

print"$1\n";

Solution 5:

The previous answer is wrong, if the HTML title tag is used more often then this can easily be overcome by checking to make sure that the title tag is valid (no tags in between).

my ($title) = $test_content =~ m/<title>([a-zA-Z\/][^>]+)<\/title>/si;

Post a Comment for "How Do I Extract An Html Title With Perl?"