Question

Is there anyway that I can extract style tag data from a HTML page using Perl

#!/usr/bin/perl
use strict;

my $HTML = <<"EOF";
    <HTML>
    <head>
    <style type='text/css'>

    #yui-dt0-bdrow0 td{background:#CFF;}

    #yui-dt0-bdrow1 td{background:#CFF;}

    #yui-dt0-bdrow2 td{background:#CFF;}

    </style>
    </head>
    </HTML>
EOF

I need to extract yui-dt0-bdrow0 td{background:#CFF;} information from the above HTML code.

I googled for lot of modules but didn't find the right one. Other than that I didn't try writing any code to extract the information

Any help is appreciated.

Was it helpful?

Solution

Use Mojo::DOM

Sample:

#!/usr/bin/perl
use strict;
use warnings;
use Mojo::DOM;
my $HTML = <<"EOF";
    <HTML>
    <head>
    <style type='text/css'>

    #yui-dt0-bdrow0 td{background:#CFF;}

    #yui-dt0-bdrow1 td{background:#CFF;}

    #yui-dt0-bdrow2 td{background:#CFF;}

    </style>
    </head>
    </HTML>
EOF
my $dom = Mojo::DOM->new( $HTML );
print $dom->find('style')->text;

Output

chankey@pathak:~/myscripts$ perl mojo.pl 


    #yui-dt0-bdrow0 td{background:#CFF;}

    #yui-dt0-bdrow1 td{background:#CFF;}

    #yui-dt0-bdrow2 td{background:#CFF;}

You can now filter out the desired data.

For a 8 minute video tutorial on Mojo::DOM and Mojo::UserAgent check out Mojocast Episode 5

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top