Here's my code -
BEGIN { $ENV{LC_ALL} = $ENV{LANG} = 'sv_SE.UTF-8' }
use strict;
use warnings;
use utf8;
use open ':std', ':locale';
use LWP::UserAgent qw( get );
use HTML::Strip qw( );
use HTML::Entities qw( decode_entities );
use Text::Sentence qw( split_sentences );
my $userAgent = LWP::UserAgent->new();
#$userAgent->agent('Mozilla/5.0');
my $url = "http://www.expressen.se";
my $response = $userAgent->get($url);
die "Can't get $url: ", $response->status_line
unless $response->is_success;
my $hs = HTML::Strip->new( decode_entities => 0 );
my $parsedContent = $hs->parse( $response->content );
utf8::decode( my $decodedParsedContent = $parsedContent );
$decodedParsedContent =~ s/(\s)+/ /g; # remove double whitespace
decode_entities(my $decodedParsedContentWithDecodeEntities = $decodedP
+arsedContent);
my @sentences = split_sentences( $decodedParsedContentWithDecodeEntiti
+es );
foreach my $sentence (@sentences)
{
#$sentence =~ s/^\s+//; # remove leading whitespace
#$sentence =~ s/\s+$//; # remove trailing
decode_entities(my $sentenceDecodeEntities = $sentence);
while ($sentenceDecodeEntities =~ /(\w+)/g)
{
print "$1 : ".$sentenceDecodeEntities."\n";
}
}
One of my output lines is -
gor : BLOGG Europe Turnéblogg Mic i vĺr replokal "The Dungeon" BLOGG L
+otta Gröning Krönikör Demokratifiasko...
Which looks good, however if I comment out either of the two calls to decode_entities(), I end up getting -
gor : BLOGG Europe Turnéblogg Mic i vĺr replokal "The Dungeon&quo
+t; BLOGG Lotta Gröning Krönikör Demokratifiasko...
Why do I need the two calls to decode_entities()??? Thanks very much for your help!
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|