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>
得到它的工作大加讚賞。 – Ebikeneser