Hi dear All,

I am trying to build a text corpus from a blog.

I have downloaded the whole site locally and removed all the files I wouldn't need, so I am left just with the html pages containing the posts (pure text, just the post: that's what I want at the end).

What follows is the smallest and cleanest code I have:

#scrape beppegrillo.it ############################################################ #!/usr/bin/perl; use utf8; use strict; use warnings; use File::Find; use HTML::Tree; use LWP::Simple; ############################################################ #set the working directory where the html files are ############################################################ my $dir = "CORPUS/2005/01/"; my $url; ############################################################# #call a sub routine in order to operate on each file in the directory ############################################################ find(\&edit, $dir); # ############################################################ #specifica cosa deve fare la subroutine edit ############################################################ sub edit() { ############################################################ #check that what you're working on is a file and not a directory; chec +k that is an html file ############################################################ my $file = $_; if ((-e $file) && (! -d $file) && (/.html?/)){ ############################################################ #open filehandle in order to read ############################################################ open (FH, "<",$file) || die $!; ############################################################ #build the tree or die ############################################################ my $tree = HTML::Tree->new(); $tree->parse_file($file) || die $!; ############################################################ #get the main div, the one that contains the post and print it as html ############################################################ my $getmaindiv = $tree->look_down(_tag => "div",id => "post_princ +ipale") || die $!; print $getmaindiv->as_HTML, "\n"; close FH; ############################################################ # ############################################################ } }

Now, it more or less works. I've been able with this code to get what I want. I had to add some more lines (but just a few, in order to get only the <p> inside the main <div> tag) but it did the work.

So, I would like to try a different approach. I would like to pile up all the code and get the most frequent sequence of tag, so to statistically identify what is NOT pure post text.

I've seen HTML::ExtractMain which should use the Readability algorithm but it doesn't seem to work.

Do you guys think that what I trying to do is possibile in a few lines of code?

At the moment I only can get all the html code togeather, but I would like to have it line by line, so that then some frequency list could be possible.

Any idea?


In reply to Scrape a blog: a statistical approach by epimenidecretese

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.