Kaustubh has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

My programming experience with perl is just 2 weeks.I am stuck with a problem.I need to get the value $8.39 from the following code snippet.This code snippet is obtained by selecting the view source option when we right click a web browser:-

<span class="details-title-text"> <strong>$8.39</strong> </span>
I am using the mechanize package.I have tried various permutations and combinations of find() & attr() methods.Basically whatever I have tried is throwing errors:-

foreach my $b ($mech->find('span')) { my $attr_class= $b->attr('class'); if(defined($attr_class) ) { my $tag_strong=$mech->find('strong'); print Dumper($tag_strong); } }

Replies are listed 'Best First'.
Re: Extracting Tag and attributes from a Web Page
by marto (Cardinal) on Jul 26, 2016 at 12:51 UTC

    Here is an example to get you started, based upon Mojo::DOM:

    use strict; use warnings; use feature 'say'; use Mojo::DOM; my $html = '<span class="details-title-text"> <strong>$8.39</strong> </span> <span class="details-title-text"> <strong>$1</strong> </span> <div> <strong>do not find me</strong> </div> <span> <strong>find this</strong> </span>'; my $dom = Mojo::DOM->new( $html ); for my $element ($dom->find('span strong')->each) { say $element->text; }

    output:

    $8.39 $1 find this

    I'll leave determining if the element text is a price as an exercise for you.

      Hi Marto! Thanks for the solution.I am a new user of this forum and I am pretty happy to get such a quick response.
Re: Extracting Tag and attributes from a Web Page
by marto (Cardinal) on Jul 27, 2016 at 07:43 UTC
      Sure will do it.Also will strike out a problem/query if it is solved.Thanks for the suggestion!

        "will strike out a problem/query if it is solved"

        Just marking it solved in the title works well, easier to read for anyone who finds your problem in future.