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

  • Home
  • SEARCH
  • 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 448457
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T21:40:17+00:00 2026-05-12T21:40:17+00:00

This is my first time using Stack Overflow, so if I’ve done something wrong

  • 0

This is my first time using Stack Overflow, so if I’ve done something wrong let me know.

I am currently trying to write a “scraper”, for lack of better term, that will extract html and replace certain inline CSS styles with the HTML counterparts. For example, I have this HTML:

<p style="text-align:center"><span style="font-weight:bold;font-style:italic;">Some random text here. What's here doesn't matter so much as what needs to happen around it.</span></p>

I want to be able to replace font-weight:bold with <b>, font-style:italic with <i>, text-align:center with <center>. Afterwards, I’ll be using regex to remove all non-basic HTML tags, and any attributes. KISS definitely applies here.

I have read this question: Convert CSS Style Attributes to HTML Attributes using Perl and a few others regarding the use of HTML::TreeBuilder and other modules (like HTML::TokeParser) but so far I’ve stumbled all over myself.

I’m new to Perl, but not new to coding in general. The logic of it is the same.

Here’s what I have so far:

#!/usr/bin/perl
use warnings;
use strict;

use HTML::TreeBuilder;

my $newcont = ""; #Has to be set to something? I've seen other scripts where it doesn't...this is confusing.
my $html = <<HTML;
<p style="text-align:center"><span style="font-weight:bold;font-style:italic;">Some random text here. What's here doesn't matter so much as what needs to happen around it.</span> And sometimes not all the text is styled the same.</p>
HTML

my $tb = HTML::TreeBuilder->new_from_content($html);
my @spans = $tb->look_down(_tag => q{span}) or die qq{look_down for tag failed: $!\n};

for my $span (@spans){
    #What next?? A print gives HASH, not really workable. Split doesn't seem to work...I've never felt like such a noobie coder before.
}

print $tb->as_HTML;

Hopefully someone can help me out, show me what I may have done wrong, etc. I’m genuinely curious as to what other possible ways there are to do this. Or if it’s ever been done before.

Also, if someone could help by suggesting which tags I should have used, that would be great. The only one I know for sure to use is perl.

  • 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-12T21:40:18+00:00Added an answer on May 12, 2026 at 9:40 pm

    By using HTML::TreeBuilder you are definitely on the right track; for parsing CSS, I’ve just found CSS::DOM. It is a really interesting module, which allows you to access properties with little effort.

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    use HTML::TreeBuilder;
    use CSS::DOM::Style;
    
    my $html = <<HTML;
    <p style="text-align:center"><span style="font-weight:bold;font-style:italic;">Some random text here. What's here doesn't matter so much as what needs to ha>
    HTML
    
    my $tb = HTML::TreeBuilder->new_from_content($html);
    
    
    my @replacements = (
        { property => 'font-style', value => 'italic', replacement => 'em' },
        { property => 'font-weight', value => 'bold', replacement => 'strong' },
        { property => 'text-align', value => 'center', replacement => 'center' },
    );
    
    # build a sensible list of tag names (or just use sub { 1 })
    my @nodes = $tb->look_down(sub { $_[0]->tag =~ /^(p|span)$/ });
    
    for my $el (@nodes) {
        if ($el->attr('style')) {
            my $st = CSS::DOM::Style::parse($el->attr('style'));
            if ($st) {
                foreach my $h (@replacements) {
                    if ($st->getPropertyValue($h->{property}) eq $h->{value}) {
                        $st->removeProperty($h->{property});
                        my $new = HTML::Element->new($h->{replacement});
                        foreach my $inner ($el->detach_content) {
                            $new->push_content($inner);
                        }
                        $el->push_content($new);
                    }
                }
                $el->attr('style', $st->cssText ? $st->cssText : undef);
            }
        }
    }
    
    print $tb->as_HTML(undef, "\t");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 231k
  • Answers 231k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Use the following function like this: Image('/path/to/original.image', '1/1', '150*', './thumb.jpg');… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer Check you database schema to see if the field (referenced… May 13, 2026 at 2:13 am
  • Editorial Team
    Editorial Team added an answer I figured out the problem - there was a session… May 13, 2026 at 2:13 am

Related Questions

This is my first time using Stack Overflow, so if I've done something wrong
This is my first time using C#, so I'm very much out of my
For my project I am using the integrated Visual Studio unit-testing framework but I
I am using simulated annealing to solve an NP-complete resource scheduling problem. For each
I frequently reboot into Windows on a bootcamp partition in my Mac Pro (e2008)

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.