2011-07-07 121 views
0

下面的Perl腳本可以在html文件中快速讀取並去掉我不需要的東西。它還打開一個空白的csv文件。獲取結果以使用Perl寫入CSV

我的問題的存在是我想要導入的剝離下來的成果轉化爲使用名稱爲字段1的CSV的3場,住在如2場和評論爲現場3

結果越來越顯示在CMD提示但不在CSV中。

use warnings; 
use strict; 
use DBI; 
use HTML::TreeBuilder; 
use Text::CSV; 

open (FILE, 'file.htm'); 
open (F1, ">file.csv") || die "couldn't open the file!"; 


my $csv = Text::CSV->new ({ binary => 1, empty_is_undef => 1 }) 
    or die "Cannot use CSV: ".Text::CSV->error_diag(); 

open my $fh, "<", 'file.csv' or die "ERROR: $!"; 
$csv->column_names('field1', 'field2', 'field3'); 
while (my $l = $csv->getline_hr($fh)) { 
    next if ($l->{'field1'} =~ /xxx/); 
    printf "Field1: %s Field2: %s Field3: %s\n", 
      $l->{'field1'}, $l->{'field2'}, $1->{'field3'} 
} 
close $fh; 

my $tree = HTML::TreeBuilder->new_from_content(do { local $/; <FILE> }); 

for ($tree->look_down('class' => 'postbody')) { 
    my $location = $_->look_down 
    ('class' => 'posthilit')->as_trimmed_text;  

    my $comment = $_->look_down('class' => 'content')->as_trimmed_text; 
    my $name  = $_->look_down('_tag' => 'h3')->as_text;  

    $name =~ s/^Re:\s*//; 
    $name =~ s/\s*$location\s*$//;  

    print "Name: $name\nLives in: $location\nCommented: $comment\n"; 
} 

的HTML的一個例子是:

<div class="postbody"> 
    <h3><a href "foo">Re: John Smith <span class="posthilit">England</span></a></h3> 
    <div class="content">Is C# better than Visula Basic?</div> 
</div> 

回答

9

其實你不寫任何東西到CSV文件。首先,你不清楚爲什麼你打開文件寫作,然後再閱讀。然後您從(現在是空的)文件讀取數據。然後你從HTML中讀取,並顯示你想要的內容。

如果您希望數據出現在其中,您肯定需要在某處寫入CSV文件!

此外,如果您想通過Text :: CSV使用它們,最好避免使用文件句柄的裸字。

也許你需要這樣的東西:

my $csv = Text::CSV->new(); 
$csv->column_names('field1', 'field2', 'field3'); 
open $fh, ">", "file.csv" or die "new.csv: $!"; 
... 
# As you handle the HTML 
$csv->print ($fh, [$name, $location, $comment]); 
... 
close $fh or die "$!"; 
+0

得到它的工作大加讚賞。 – Ebikeneser