Hello Lisa1993, and welcome to the Monastery!

As Corion says, you’d be better off using a dedicated module to extract the HTML you want. But in the meantime...

hippo has identified the syntax errors in your code. But, even when these are fixed, the regular expression won’t match any of the content on the web page in question. To get it to match, I had to tweak it in two places:

use strict; use warnings; use LWP::Simple; my $URL = 'https://www.reddit.com/r/unitedkingdom/comments/58m2hs/ +' . 'i_daniel_blake_is_released_today/'; my $CONTENT = get($URL); my $regex = '<div class="usertext-body may-blank-within md-container + ">' . '<div class="md">(.+?)</div>\s*</div>' . '</form><ul class="flat-list buttons">'; my $x = ''; my $count = 0; while ($CONTENT =~ m{$regex}gs) { $x .= $1; ++$count; } print $x; print "Count: $count\n";

First, you need to allow for (optional) whitespace between the two closing </div> tags. Second, you need to remove the space at the end of the regex. Also note that it isn’t necessary to escape the quotation character, and you can avoid escaping forward slashes by changing the regex delimiter (as shown above).

With these changes, I get 80 matches.

Also note the inclusion of use strict and use warnings, and the declaration of variables using my. This is basic good practice in Perl.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Question regarding web scraping by Athanasius
in thread Question regarding web scraping by Lisa1993

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.