Hello everybody,

I am very very new to using Perl (as in have only been using it intermittently for less than a couple of weeks), and I'm having a problem with a very basic web scraping programme.

To cut a long story short, I am taking a university module where the instructor has told us to scrape comments from the website Reddit, so that we can analyse the comments using computational linguistics methods.

He has given us the code below to use. The goal is to get the programme to search for every comment on the page and then to save the comments only in an html file.
use LWP::Simple; $URL = 'https://www.reddit.com/r/unitedkingdom/comments/58m2hs/i_danie +l_blake_is_released_today/'; $CONTENT = get($URL); while ($CONTENT =~ <div class=\"usertext-body may-blank-within md-cont +ainer \"><div class=\"md\">(.+?)<\/div> <\/div><\/form><ul class=\"flat-list buttons\"> //gs ) { $x = "$1"; $y = "$y $x"; } open(OUT, "">C:/Users/user/perl_tests/reddittest.txt"); print OUT "$y"; close(OUT);
However, when I try to run the code (from my windows command centre) I get the following error message:
Microsoft Windows [Version 6.1.7601] Copyright (c) 2009 Microsoft Corporation. All rights reserved. C:\Users\user>perl perl_tests/red1.pl Scalar found where operator expected at perl_tests/red1.pl line 12, ne +ar "$x = " $1" (Might be a runaway multi-line "" string starting on line 7) (Missing operator before $1?) String found where operator expected at perl_tests/red1.pl line 12, ne +ar "$y = " " (Missing semicolon on previous line?) Scalar found where operator expected at perl_tests/red1.pl line 12, ne +ar "$y $x" (Missing operator before $x?) String found where operator expected at perl_tests/red1.pl line 12, ne +ar "open(O UT, "" (Missing semicolon on previous line?) String found where operator expected at perl_tests/red1.pl line 15, ne +ar "open(O UT, "">C:/Users/user/perl_tests/reddittest.txt"" Can't modify numeric lt (<) in scalar assignment at perl_tests/red1.pl + line 12, near "$x = "$1" syntax error at perl_tests/red1.pl line 12, near "$x = "$1" Execution of perl_tests/red1.pl aborted due to compilation errors.
I know that there is probably a very "basic" error in the code, but I'm too inexperienced as of yet to know how to fix it. Any help would be greatly appreciated. Thank you!

In reply to 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.