Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3216384
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:17:17+00:00 2026-05-17T15:17:17+00:00

Folks, There is so much info out there on HTML::Treebuilder that I’m surprised I

  • 0

Folks,

There is so much info out there on HTML::Treebuilder that I’m surprised I can’t find the answer, hopefully I’m not just missing it.

What I’m trying to do is simply parse between parent nodes, so given a html doc like this

<html>
<body>
   <a id="111" name="111"></a>
   <p>something</p>
   <p>something</p>
   <p>something</p>
   <a href=xxx">something</a>
   <a id="222" name="222"></a>
   <p>something</p>
   <p>something</p>
   <p>something</p>
   ....
 </body>
 </html>

I want to be able to get the info about that 1st anchor tag (111), then process the 3 p tags and then get the next anchor tag (222) and then process those p tags etc etc.

Its easy to get to each anchor tag

use HTML::TreeBuilder;
my $tree = HTML::TreeBuilder->new();
$tree->parse_file("index-01.htm");
foreach my $atag ( $tree->look_down( '_tag', 'a' ) ) {
    if ($atag->attr('id')) {
        # Found 'a' tag, now process the p tags until the next 'a'
    }
}

But once I find that tag how do I then get all the p tags until the next anchor?

TIA!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-17T15:17:17+00:00Added an answer on May 17, 2026 at 3:17 pm

    HTML::TreeBuilder version

    #!/usr/bin/perl
    
    use strict; use warnings;
    use HTML::TreeBuilder;
    
    my $tree = HTML::TreeBuilder->new;
    
    $tree->parse_file(\*DATA);
    $tree->elementify;
    $tree->objectify_text;
    
    foreach my $atag ( $tree->look_down( '_tag', 'a' ) ) {
        if ($atag->attr('id')) {
            printf "Found %s\n", $atag->as_XML;
            process_p( $atag );
        }
    }
    
    sub process_p {
        my ($tag) = @_;
        while ( defined( $tag ) and defined( my $next = $tag->right ) ) {
            last if lc $next->tag eq 'a';
            if ( lc $next->tag eq 'p') {
                $next->deobjectify_text;
                print $next->as_text, "\n";
            }
            $tag = $next;
        }
    }
    
    __DATA__
    <html>
    <body>
       <a id="111" name="111"></a>
       <p>something</p>
       <p>something</p>
       <p>something</p>sometext
       <a href=xxx">something</a>
       <a id="222" name="222"></a>
       <p>something</p>
       <p>something</p>
       <p>something</p>
     </body>
     </html>
    

    Output:

    Found <a id="111" name="111"></a>
    
    something
    something
    something
    Found <a id="222" name="222"></a>
    
    something
    something
    something
    

    HTML::TokeParser::Simple version

    #!/usr/bin/perl
    
    use strict; use warnings;
    use HTML::TokeParser::Simple;
    
    my $parser = HTML::TokeParser::Simple->new(\*DATA);
    
    while ( my $tag = $parser->get_tag('a') ) {
        next unless $tag->get_attr('id');
        printf "Found %s\n", $tag->as_is;
        process_p($parser);
    }
    
    sub process_p {
        my ($parser) = @_;
        while ( my $next = $parser->get_token ) {
            if ( $next->is_start_tag('a') ) {
                $parser->unget_token($next);
                return;
            }
            elsif ( $next->is_start_tag('p') ) {
                print $parser->get_text('/p'), "\n";
            }
        }
        return;
    }
    

    Output:

    Found <a id="111" name="111">
    something
    something
    something
    Found <a id="222" name="222">
    something
    something
    something
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Folks, is there somewhere on the Net where I can find a list of
Folks, is there a collection of gotchas where Numpy differs from python, points that
Hello folks! Could you tell me a good(non-redundant) way to find out the following:
I ran across something that I eventually figured out, but think that there's probably
Folks, I've got an ASP.NET MVC application that I am attempting to secure using
Folks. I trust that the community will see this as a relevant question. My
I've noticed that a lot of folks here cite tables with 20+ (I've seen
Hey folks, I have a question which feels stupid but I can't quite say
I have an app that uses several prefixes and, while not frequent, it's also
Disclaimer, I'm from a Java background. I don't do much C#. There's a great

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.