3

我的目标是从以下站点中标题为“激动剂”、“拮抗剂”和“变构调节剂”的表格中提取链接:

http://www.iuphar-db.org/DATABASE/ObjectDisplayForward?objectId=1&familyId=1

我一直在使用 HTML::TableExtract 来提取表格,但无法获取 HTML::LinkExtor 来检索有问题的链接。这是我到目前为止的代码:

use warnings;
use strict;
use HTML::TableExtract;
use HTML::LinkExtor;

my @names = `ls /home/wallakin/LINDA/ligands/iuphar/data/html2/`; 

foreach (@names)
{
chomp ($_);

my $te = HTML::TableExtract->new( headers => [  "Ligand", 
                        "Sp.", 
                        "Action", 
                            "Affinity", 
                        "Units",
                        "Reference" ] );
my $le = HTML::LinkExtor->new();

$te->parse_file("/home/wallakin/LINDA/ligands/iuphar/data/html2/$_");

my $output = $_;
$output =~ s/\.html/\.txt/g;
open (RESET, ">/home/wallakin/LINDA/ligands/iuphar/data/links/$output") or die "Can't reset";
close RESET;
#open (DATA, ">>/home/wallakin/LINDA/ligands/iuphar/data/links/$output") or die "Can't append to file";

foreach my $ts ($te->tables)
{
    foreach my $row ($ts->rows)
    {
        $le->parse($row->[0]);
        for my $link_tag ( $le->links ) 
        {
            my %links = @$link_tag;
            print @$link_tag, "\n";
            }
        }
}
#print "Links extracted from $_\n";
}

我尝试使用该站点上另一个线程的一些示例代码(Perl parse links from HTML Table)无济于事。我不确定这是解析问题还是表格识别问题。提供的任何帮助将不胜感激。谢谢!

4

2 回答 2

4

试试这个作为一个基本脚本(你只需要调整它来获取链接):

use warnings; use strict;
use HTML::TableExtract;
use HTML::LinkExtor;
use WWW::Mechanize;

use utf8;
binmode(STDIN, ":utf8");
binmode(STDOUT, ":utf8");
binmode(STDERR, ":utf8");

my $m = WWW::Mechanize->new( autocheck => 1, quiet => 0 );
$m->agent_alias("Linux Mozilla");
$m->cookie_jar({});

my $te = HTML::TableExtract->new(
    headers => [
        "Ligand",
        "Sp.",
        "Action",
        "Affinity",
        "Units",
        "Reference"
    ]
);

$te->parse(
    $m->get("http://tinyurl.com/jvwov9m")->content
);

foreach my $ts ($te->tables) {
    print "Table (", join(',', $ts->coords), "):\n";
    foreach my $row ($ts->rows) {
        print join(',', @$row), "\n";
    }
}
于 2013-08-14T20:12:29.983 回答
2

你没有描述问题是什么......究竟是什么不起作用?包含什么$row->[0]?但问题的一部分可能是 TableExtract 默认只返回“可见”文本,而不是原始 html。您可能希望在 HTML::TableExtract 中使用 keep_html 选项。

于 2013-08-14T19:54:48.807 回答