CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Need a Custom or Prewritten Perl Program?: I need a program that...:
Trying to get meta tags

 



Regent
New User

Nov 19, 2004, 1:59 PM

Post #1 of 5 (2983 views)
Trying to get meta tags Can't Post

Hi,

I need to grab a web page then print the title and meta description. I've already been able to print out the title using the following:

my $h = HTTP::Headers->new;
my $p = HTML::HeadParser->new($h);
$p->parse($html) and print "not finished";

$p->header('Title');
print $h->title;
print $h->title;

But I can't figure out how to print out the meta description using HTTP::Headers and HTML::HeadParser. Is there any way to do this using these packages? I don't want to bother with regular expressions.

Thanks.


(This post was edited by Regent on Nov 19, 2004, 2:04 PM)


zOrK
Novice

Jul 26, 2007, 11:09 PM

Post #2 of 5 (2488 views)
Re: [Regent] Trying to get meta tags [In reply to] Can't Post

try

Code
#!/usr/bin/perl 
use strict;
use HTML::TokeParser;

sub get_description {
my $content;
my $p = HTML::TokeParser->new($_[0]);
while ( my $tag = $p->get_tag("meta") ) {
if ( defined( $tag->[1]{'name'} )) {
if($tag->[1]{'name'} eq "description") {
$content = $tag->[1]{'content'}
}
};
}
print "$content\n";
}

get_description("index.html");


You need http://search.cpan.org/CPAN/authors/id/G/GA/GAAS/HTML-Parser-3.56.tar.gz

---------

You may also find Perl information on my blog: http://www.ferry.cl/carlos/

(This post was edited by zOrK on Jul 26, 2007, 11:10 PM)


KevinR
Veteran


Jul 27, 2007, 3:20 AM

Post #3 of 5 (2485 views)
Re: [zOrK] Trying to get meta tags [In reply to] Can't Post

Nice reply but did you notice this question is going on 3 years old? Shocked
-------------------------------------------------


zOrK
Novice

Jul 27, 2007, 7:08 AM

Post #4 of 5 (2484 views)
Re: [Regent] Trying to get meta tags [In reply to] Can't Post

I realized that too late, It was funny. It may help somebody browsing the web. (hope so).

---------

You may also find Perl information on my blog: http://www.ferry.cl/carlos/


KevinR
Veteran


Jul 27, 2007, 10:54 AM

Post #5 of 5 (2482 views)
Re: [zOrK] Trying to get meta tags [In reply to] Can't Post

Hopefully it will. Smile
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives