Hi mserino,

While you could certainly get at an individual element of the list returned, it would be cumbersome to access it that way in your example page, where there are more than 600 <p> tags. I think you would be better served searching for a match in the paragraphs you want. Here's an example:

#!/usr/bin/perl use strict; use warnings; use feature qw/ say /; use Data::Dumper; use WWW::Mechanize; use WWW::Mechanize::TreeBuilder; my $mech = WWW::Mechanize->new(); WWW::Mechanize::TreeBuilder->meta->apply($mech); $mech->get("http://millercenter.org/president/clinton/oralhistory/roge +r-altman"); for ( $mech->look_down(_tag => 'p') ) { next unless $_->as_trimmed_text =~ m/a haircut in California/; say $_->as_HTML; } __END__
Output:
<p>The election occurred and about a week later Reagan was getting a h +aircut in California and he came out of the barbershop. There had bee +n some minor event that day and he was asked what he thought of the l +atest developments in Iran and he said, <q>I don&rsquo;t talk about b +arbarians.</q>

No problem to build up a list of paragraphs and push them onto your output, then get some other content. You can definitely do it in one program. Hope this helps!

The way forward always starts with a minimal test.

In reply to Re: Extracting specific <p>content</p> using WWW::Mechanize by 1nickt
in thread Extracting specific <p>content</p> using WWW::Mechanize by mserino

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.