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 6100803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:25:39+00:00 2026-05-23T13:25:39+00:00

I have an html page that has particular text that I want to parse

  • 0

I have an html page that has particular text that I want to parse into a databse using a Perl Script.

I want to be able to strip off all the stuff I don’t want, an exmple of the html is-

<div class="postbody">
        <h3><a href "foo">Re: John Smith <span class="posthilit">England</span></a></h3>
        <div class="content">Is C# better than Visula Basic?</div>
    </div>

Therefore I would want to import into the database

  1. Name: John Smith.
  2. Lives in: England.
  3. Commented: Is C# better than Visula Basic?

I have started to create a Perl script but it needs to be changed to work for what I want;

    use DBI;

    open (FILE, "list") || die "couldn't open the file!";

    open (F1, ">list.csv") || die "couldn't open the file!";

    print F1 "Name\|Lives In\|Commented\n";

    while ($line=<FILE>)

    {

    chop($line);
    $text = "";
    $add = 0;
    open (DATA, $line) || die "couldn't open the data!";
    while ($data=<DATA>)

    {
    if ($data =~ /ds\-div/)
    {
    $data =~ s/\,//g;
    $data =~ s/\"//g;
    $data =~ s/\'//g;
    $text = $text . $data;
    }

    }

    @p = split(/\\/, $line);
    print F1 $p[2];
    print F1 ",";
    print F1 $p[1];
    print F1 ",";
    print F1 $p[1];
    print F1 ",";  

    print F1 "\n";
    $a = $a + 1;

Any input would be greatly appreciated.

  • 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-23T13:25:40+00:00Added an answer on May 23, 2026 at 1:25 pm

    Please do not use regular expressions to parse HTML as HTML is not a regular language. Regular expressions describe regular languages.

    It is easy to parse HTML with HTML::TreeBuilder (and its family of modules):

    #!/usr/bin/env perl
    
    use warnings;
    use strict;
    
    use HTML::TreeBuilder;
    
    my $tree = HTML::TreeBuilder->new_from_content(
        do { local $/; <DATA> }
    );
    
    for ( $tree->look_down( 'class' => 'postbody' ) ) {
        my $location = $_->look_down( 'class' => 'posthilit' )->as_trimmed_text;
        my $comment  = $_->look_down( 'class' => 'content' )->as_trimmed_text;
        my $name     = $_->look_down( '_tag'  => 'h3' )->as_trimmed_text;
        $name =~ s/^Re:\s*//;
        $name =~ s/\s*$location\s*$//;
    
        print "Name: $name\nLives in: $location\nCommented: $comment\n";
    }
    
    __DATA__
    <div class="postbody">
        <h3><a href="foo">Re: John Smith <span class="posthilit">England</span></a></h3>
        <div class="content">Is C# better than Visual Basic?</div>
    </div>
    

    Output

    Name: John Smith
    Lives in: England
    Commented: Is C# better than Visual Basic?
    

    However, if you require much more control, have a look at HTML::Parser as has already been answered by ADW.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rails app that has some simple functionality, allowing the user to
I have some Javascript code that needs to be dynamically created as a String
I am creating one application with jquery.mobile-1.0a4.1 release. I have a page which is
I have a page which uses jQuery for some AJAX-stuff und Django as main
Really simple question. I trying to test a Restful webservice that I am developing,
I'm having some issues with attaching live event handlers to particular rows. What I
I know a variation on this has been asked several times; I've been browsing
I am having an issue with creating an XLSX document with the Open XML
I've looked high and low for an answer to this and I'm hoping someone
I use @RenderSection(Contextual, false) within my _Layout.cshtml to allow different views to render their

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.