Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
use strict; use warnings; use LWP::Simple; use HTML::Strip; my $url = "http://www.expressen.se/ekonomi/1.1805368/reinfeldt-forsvar +ar-nya-sjukregler"; my $html = get($url); defined $html or die "Can't fetch HTML from: ",$url; my $hs = HTML::Strip->new(); my $clean_text = $hs->parse( $html ); $hs->eof; #sentences spread across multiple lines $clean_text =~ s/(\n)/ /g; $clean_text =~ s/(\r)/ /g; #compact double white space into dots $clean_text =~ s/(\s){2,}/./g; #compact dots into single dots $clean_text =~ s/(\.){2,}/\./g; #split sentances on dots my @sentences = split(/\./, $clean_text); my @realSentences = (); #ignore sentences with single words foreach my $sentence (@sentences) { if(($sentence =~ tr/ //)>1) { push @realSentences, $sentence; } } foreach (@realSentences) { print "$_\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Extracting the sentences from a webpage
by kyle (Abbot) on Dec 07, 2009 at 22:31 UTC | |
|
Re: Extracting the sentences from a webpage
by Lu. (Hermit) on Dec 07, 2009 at 22:35 UTC | |
by toey (Initiate) on Dec 08, 2009 at 00:43 UTC |