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

Greetings avout, I'm trying to extract the subject (between the h3 tags) in the following example using Web::Query. No matter what I try $subject is always undefined. What have I missed? UPDATE: find 'h3' returns the author text, but I want to get the h3 in the subject class. I tried .subject.div.h3 but still undef.

#!/usr/bin/perl use strict; use warnings; use Web::Query; # libweb-query-perl use Data::Dumper; my $testhtml =' <html><head></head> <body> <div class="author" <div><h3>Neil Watson</h3></div> </div> <div class="subject"> <div><h3>@if version_after macro is illogical</h3></div> </div> </body> </html> '; my $parts = Web::Query->new_from_html( $testhtml ); my $subject = $parts->find( 'h3' )->text; # How to find the subject between h3 tags? #print "parts ".Dumper( $parts ); print "subjectfinal ".Dumper( $subject );

Neil Watson
watson-wilson.ca

Replies are listed 'Best First'.
Re: Getting content using Web::Query
by afoken (Chancellor) on Jun 17, 2015 at 19:04 UTC
    <div class="subject"> my $subject = $parts->find( '#div-subject' )->html;

    This looks like a mismatch. find() expects a CSS3 selector. #div-subject selects all elements with the ID div-subject. To select a div element with class subject, use div.subject instead.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

      Do you mean my $subject = $parts->find( 'div.subject' )->html;? That returns undef too.

      Neil Watson
      watson-wilson.ca

        Perhaps try ->filter instead of ->find?

        Dum Spiro Spero
        That returns undef too.
        Are you sure? For me it returns:
        '<div><h3>@if version_after macro is illogical</h3></div>';
Re: Getting content using Web::Query
by tangent (Parson) on Jun 18, 2015 at 02:13 UTC
    I tried .subject.div.h3 but still undef
    You need to write the selector just as you would if writing CSS, so:
    my $subject = $parts->find( '.subject div h3' )->text; but this also works: my $subject = $parts->find( 'div.subject' )->text;