Parsing HTML with HTML::Tree

Discussion in 'Perl Misc' started by Ninja Li, Mar 1, 2010.

  1. Ninja Li

    Ninja Li Guest

    Hi,

    I am trying to parsing the following HTML content:

    -- first part
    <td class="storyTitle"> @0.1.7.4.0.0.5.0.0.11.1
    <a href="/GeneralContent/MySearch.aspx?PagePrefix=IN&amp;
    target="_new"> @0.1.7.4.0.0.5.0.0.11.1.0
    "Chicago"

    -- second part
    <td class="storyTitle"> @0.1.7.4.0.0.5.0.0.17.1
    <b> @0.1.7.4.0.0.5.0.0.17.1.0
    "Something here"

    I am using HTML:Tree to parse the HTML and what I would like to do is
    that whenever there isn't any <a href=.....> segment as in the second
    part of the HTML, I will print something else, such as "Error
    occurred". Notice that both first and second parts of the HTML have
    common text of "<td class="storyTitle">", which I use for search
    criteria.

    My problem is that I don't know what the following code will return
    whenever <a href=...> is not found. I tried to test against "" or
    undef, but doesn't seem to work.

    The following is some of my code and it doesn't work as I wish.

    use strict;
    use LWP::Simple;
    use HTML::Tree;

    if ($td->attr('class') eq 'storyTitle')
    {
    if (my $sym = $td->find('a'))
    {
    if ($sym->as_text() ne '')
    {
    print $sym->as_text() . "\n";
    }
    else
    {
    print "Error Occurred" . "\n";
    }
    }
    }
    Ninja Li, Mar 1, 2010
    #1
    1. Advertising

  2. Ninja Li

    Ninja Li Guest

    Tad,

    Thanks for your advice. You hit the nail on the head and it works
    well now.

    Nick
    Ninja Li, Mar 1, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    2
    Views:
    1,583
    Roedy Green
    Aug 16, 2005
  2. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    0
    Views:
    419
    Ramkumar Menon
    Aug 16, 2005
  3. Ramkumar Menon

    B+ Tree versus Ternary Search Tree

    Ramkumar Menon, Aug 16, 2005, in forum: Java
    Replies:
    1
    Views:
    436
    Andrew Thompson
    Aug 16, 2005
  4. Stub

    B tree, B+ tree and B* tree

    Stub, Nov 12, 2003, in forum: C Programming
    Replies:
    3
    Views:
    10,088
  5. Eli Bendersky

    Parsing HTML into a tree

    Eli Bendersky, Apr 25, 2007, in forum: Ruby
    Replies:
    1
    Views:
    74
    Ganesh Gunasegaran
    Apr 25, 2007
Loading...

Share This Page