Ebi*_*ser 1 html database perl extract
我有一个html页面,其中包含我想要使用Perl脚本解析为数据库的特定文本.
我希望能够剥离我不想要的所有东西,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>
Run Code Online (Sandbox Code Playgroud)
因此我想导入数据库
我已经开始创建一个Perl脚本,但需要将其更改为我想要的工作;
use DBI;
open (FILE, "list") || die "couldn't open the file!";
open (F1, ">list.csv") || die "couldn't open the file!";
print F1 "Name\|Lives In\|Commented\n";
while ($line=<FILE>)
{
chop($line);
$text = "";
$add = 0;
open (DATA, $line) || die "couldn't open the data!";
while ($data=<DATA>)
{
if ($data =~ /ds\-div/)
{
$data =~ s/\,//g;
$data =~ s/\"//g;
$data =~ s/\'//g;
$text = $text . $data;
}
}
@p = split(/\\/, $line);
print F1 $p[2];
print F1 ",";
print F1 $p[1];
print F1 ",";
print F1 $p[1];
print F1 ",";
print F1 "\n";
$a = $a + 1;
Run Code Online (Sandbox Code Playgroud)
任何投入将不胜感激.
请不要使用正则表达式来解析HTML,因为HTML不是常规语言.正则表达式描述常规语言.
使用HTML::TreeBuilder(及其模块系列)解析HTML很容易:
#!/usr/bin/env perl
use warnings;
use strict;
use HTML::TreeBuilder;
my $tree = HTML::TreeBuilder->new_from_content(
do { local $/; <DATA> }
);
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_trimmed_text;
$name =~ s/^Re:\s*//;
$name =~ s/\s*$location\s*$//;
print "Name: $name\nLives in: $location\nCommented: $comment\n";
}
__DATA__
<div class="postbody">
<h3><a href="foo">Re: John Smith <span class="posthilit">England</span></a></h3>
<div class="content">Is C# better than Visual Basic?</div>
</div>
Run Code Online (Sandbox Code Playgroud)
Name: John Smith
Lives in: England
Commented: Is C# better than Visual Basic?
Run Code Online (Sandbox Code Playgroud)
但是,如果您需要更多控制,请查看ADWHTML::Parser已经回答的问题.