2010-10-16 69 views
0

我必须解析5000个文件 - 看起来非常相似。如何使用Perl从HTML源代码中提取特定表格的内容?

我喜欢使用HTML::TokeParser::SimpleDBI以执行解析作业并存储结果。

我几乎没有HTML::TokeParser::Simple的经验,但是这个任务超过了 我的头。注意:我也看过了这些想法 - 这似乎也是一种适当的方式。但目前我有问题需要获取相应的xpath-expressions:我试图确定需要在Perl程序中填充的相应xpath表达式。

这就是我现在所拥有的:

use strict; 

use HTML::TreeBuilder::XPath; 

my $tree = HTML::TreeBuilder::XPath->new; 

#use real file name here 
open(my $fh, "<", "file.html") or die $!; 

$tree->parse_file($fh); 

my ($name)  = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($type)  = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($adress)  = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($adress_two) = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($telephone) = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($fax) = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($internet) = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($officer) = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($employees) = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($offices) = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($worker)  = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($country) = $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 
my ($the_council)= $tree->findnodes(qq{/html/body/table/tr[1]/td[2]}); 


print $name->as_text; 
print $type->as_text; 
print $adress->as_text; 
print $adress_two->as_text; 
print $telephone->as_text; 
print $fax->as_text; 
print $internet->as_text; 
print $officer->as_text; 
print $employees->as_text; 
print $offices->as_text; 
print $worker->as_text; 
print $country->as_text; 
print $the_council->as_text; 

这一切吗?注意 - 我将它存储在数据库中。

BTW:参见示例站点之一:

http://www.kultusportal-bw.de/servlet/PB/menu/1188427/index.html?COMPLETEHREF=http://www.kultus-bw.de/did_abfrage/detail.php?id=04313488

在您看到想要的信息灰色阴影块

:被通缉17行。请注意 - 我有5000个不同的HTML文件 - 所有文件的结构都是一样的!

这意味着我会很高兴有一个可以用HTML :: TokeParser :: Simple和DBI运行的模板。

我可以使用上面提到的代码...或者我必须改变它。

喜欢听到你的声音!那太好了!!

+0

仅供参考:我试图编辑您的问题,但我无法理解标题。请尝试拿出一些描述性的东西。 – 2010-10-16 13:59:49

+1

参见http://stackoverflow.com/q/3946874/100754 – 2010-10-16 14:12:18

回答

3

使用一些魔法HTML::TableExtract

#!/usr/bin/perl 

use strict; use warnings; 
use HTML::TableExtract; 
use YAML; 

my $te = HTML::TableExtract->new(attribs => { 
    border => 0, 
    bgcolor => '#EFEFEF', 
    leftmargin => 15, 
    topmargin => 5, 
}); 

$te->parse_file('kultus-bw.html'); 
my ($table) = $te->tables; 

for my $row ($table->rows) { 
    cleanup(@$row); 
    print "@$row\n"; 
} 

sub cleanup { 
    for (@_) { 
     s/\s+//; 
     s/[\xa0 ]+\z//; 
     s/\s+/ /g; 
    } 
} 

输出:

Schul-/Behördenname: Abendgymnasium Ostwürttemberg 
Schulart: Privatschule (04313488) 
Hausadressse: Friedrichstr.70, 73430 Aalen 
Postfachadresse: Keine Angabe 
Telefon: 07361/680040 
Fax: 07361/680040 
E-Mail: Keine Angabe 
Internet: www.abendgymnasium-ostwuerttemberg.de 
ÜbergeordneteDienststelle: Regierungspräsidium Stuttgart Abteilung 7 Schule und Bildung 
Schulleitung: Keine Angabe 
Stellv.Schulleitung: Keine Angabe 
AnzahlSchüler: 259 
AnzahlKlassen: 8 
AnzahlLehrer: Keine Angabe 
Kreis: Ostalbkreis 
Schulträger: <Verband/Verein> (Verband/Verein) 

当然,我运行脚本之前保存的网页的本地副本。

+0

你好SinanÜnür,那太好了。你做得比预期的多。我很高兴。这正是我想要的结果。我不知所措。你建议的这个代码完成了所有的伎俩! – zero 2010-10-16 16:47:48

+0

再次思南Ünur,我很兴奋。我必须理解代码。你会得到很棒的输出。我喜欢了解这个伟大的代码 - 这是能够给这个ouptut!做得好。 - 我今天晚些时候回来。现在我已经离开了两个小时的房子。但是我确定我回来了!许多人再次感谢!问候马丁 – zero 2010-10-16 17:06:26

+0

再次我 - 试图纠正线程标题。系统否认,因为我是新用户。 Martin – zero 2010-10-16 17:09:32

相关问题