Attached below is a PERL snippet that gets a specific recipe form allrecipes.com and writes the xml source code to a txt file.

#! C:/Perl64/bin/perl # Calls the PERL interpreter use strict; #use features that provide detailed warning and cleanup + use warnings; #use features that provide detailed warning and cleanup + use autodie; #sometimes prevents the program from hanging and kills i +t use LWP::Simple; # PERL module to connect to Internet my $out_file = 'recipe.txt'; #Defines output my $encoding = ":encoding(UTF-8)"; #Defines encoding type for text t +ypical of western webpages open (my $handle2, ">> $encoding", $out_file) || die "Could not open $ +out_file: $!"; #Opens output and assigns an internal name independe +nt of file name my $content=get("http://allrecipes.com/recipe/11253/mock-peanut-brittl +e/") or die "ouch"; #Gets the webpage as xml print $handle2 $content."\n"; #Writes the URL as text to the output +file exit;

My question is how i can build a program with this snippet that loops through a series of recipes (lets say from recipe 11253 to 11300) and writes the individual xml code to separate files, each having a variable file name. So i just have to insert a loop somewhere in this code that pulls the source code of each recipe in the range I specify from Allrecipes.com and dumps the text into a file that is than collected into one of my directories containing all the recipe files. I am getting the recipe ID numbers from the allrecipes url. Every recipe has a unique number, so www.allrecipes.com/recipe/10413/ for example, is an easy valentine day cookie recipe. i know that I will need to insert a sleep command between url fetches or i will be flagged as a bot and kicked out. And that it needs to be at some random time. It will probably look like: sleep(rand(10 also, i have to choose a sensible way of naming each recipe file In the end, I should be able to have 10,000 named txt files of the recipes I specify in a directory. After this step is when i will parse the info that I need form the text to do my analysis.


In reply to Perl program for webscraping by Emmasue

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.