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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |