perlquestion
marcoss
<p>Hi, I need to extract certain pieces of information from a website. There's a <code><p></code> tag with 5 <code><span></code> tags inside it. One of the spans has a class, so no problem, but the other 4 are just <code><span>info</span></code>. This is how the code looks in the website. I'm using Firebug.</p>
<code>
<p class="itinerari-info">
<span>
<b> Itinerario </b>
Danimarca, fiordi norvegesi, Germania
</span>
<span class="DepartureDateTitle">
<b>Data partenza</b>
17 giugno 2012
</span>
<span>
<b> Nave </b>
Costa Fortuna
</span>
<span>
<b> N.ro giorni crociera </b>
7
</span>
<span>
<b> Porto di partenza </b>
Copenhagen
</span>
</p>
</code>
<readmore>
<p>My perl knowledge is limited to the first nine chapters of "Learning Perl" (and that doesn't mean I understand everything, especially sub routines) I don't have any other programming skills.</p>
<p>This is the code I have so far:</p>
<code>
#!/usr/bin/perl -w
use LWP::Simple;
use HTML::TreeBuilder;
use strict;
my $base='http://www.costacrociere.it';
my $url='/it/lista_crociere/capitali_nord_europa-201206.html';
my $page = get($base.$url) or die $!;
my $p = HTML::TreeBuilder->new_from_content( $page );
my @trips= $p->look_down(_tag=>'p',class=>'itinerari-info')->as_text;
foreach my $trip (@trips){
print $trip;
}
</code>
<p>And this is the output:</p>
<code>Itinerario Danimarca, fiordi norvegesi, Germania Data partenza 17�giugno�2012 Nave Costa Fortuna N.ro giorni crociera � 7 Porto di partenza Copenhagen Documenti di viaggio Passaporto�o�Carta d'identit� valida per l'espatrio Possono essere disponibili le seguenti tariffe</code>
<p>So, this outputs all the information in one string and also with some strange characters, but I can use a regex to fix that. The main issue is that I need every string to be independent from each other (As if I wanted to add a title prior to each information itself). I see that the spans have <code><b>whatever</b></code> tags, but I just can't seem to understand how I could use those to do what I want. Like I said, my experience is close to zero. I've been trying different stuff with arrays and hashes and right now I just want to burn the computer. If the Monks could help me I would greatly appreciate it. Thank you so much!</p>
</readmore>