kwtow has asked for the wisdom of the Perl Monks concerning the following question:

Hi being searching thru this forum and also cpan but failed to find what I want

I want to extract the attribute of a tag like <a class='team' href='javascript:void(0)' title='(PEN.66) O FLYNN RILEY (51)'>(PEN.66) O FLYNN,</font></a>

Instead of the href attribute that most people want I m interested in the value for the title attribute. Can anyone help me in this?

Replies are listed 'Best First'.
Re: extracting attribute from HTML tag
by atcroft (Abbot) on May 25, 2003 at 17:30 UTC

    If you look at HTML::TokeParser Tutorial 's example code, especially around line 30 or following, it should do what you need if you change 'href' to 'title' on line 33, I believe.

    Hope that helps.

Re: extracting attribute from HTML tag
by arthas (Hermit) on May 25, 2003 at 17:38 UTC
    You can use HTML::TokeParser::Simple, which does exactly what you need (you can get the value of any attribute of a given tag).

    This module is actually an interface to HTML::TokeParser. It's simpler in the sense that it's easier to fetch the data about the tags: you have many methods using which you can avoid to parse the TokeParser's output.

    Michele.
Re: extracting attribute from HTML tag
by svsingh (Priest) on May 26, 2003 at 06:37 UTC
    If that's all you're trying to do, then a simple string match should work for you. For example:
    $s = "(<a class='team' href='javascript:void(0)' title='(PEN.66) O FLY +NN RILEY (51)'>"; $s =~ /title='([^']+)'/; print "title: $1\n";

    That returns:

    title: (PEN.66) O FLYNN RILEY (51)

    For other similar examples, you may want to check out Teach Yourself Perl in 21 Days: Day 10 (img.pl). That example shows a decent (but not comprehensive) way to extract attributes from HTML tags.

    Hope this helps.