私はPerl/HTMLにかなり慣れていません。これが私がWWW::MechanizeとHTML::TreeBuilderでやろうとしていることです:
ウィキペディアの化学元素のページごとに、ウィキの他の化学元素のページを指すすべてのハイパーリンクを抽出し、それぞれの固有のペアを次の形式で出力する必要があります。
Atomic_Number1 (Chemical Element Title1) -> Atomic_Number2 (Chemical Element Title2)
唯一の問題は、すべての化学元素のページ (ページの右上) にミニ周期表があることです。したがって、この小さな周期表では、すべての元素について同じ結果が得られます。そのテーブルを除いて、ページからすべてのリンクを抽出するのに問題があります。
[注:$elem == 6
デバッグを容易にするために、(Carbon) (@line 42) のみを調べました。]
これが私のコードです:
#!/usr/bin/perl -w
use strict;
use warnings;
use WWW::Mechanize;
use HTML::TreeBuilder;
my $mech = WWW::Mechanize->new( autocheck => 1 );
$mech = WWW::Mechanize->new();
my $table_url = "http://en.wikipedia.org/wiki/Periodic_table";
$mech->agent('Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_5; en-us) /
AppleWebKit/533.17.8 (KHTML, like Gecko) Version/5.0.1 /
Safari/533.17.8');
$mech->get($table_url);
my $tree = HTML::TreeBuilder->new_from_content($mech->content);
my %elem_set;
my $atomic_num;
## obtain a hash array of elements and corresponding titles and links
foreach my $td ($tree->look_down(_tag => 'td')) {
# If there's no <a> in this <td>, then skip it:
my $a = $td->look_down(_tag => 'a') or next;
my $tdText = $td->as_text;
my $aText = $a->as_text;
if($tdText =~ m/^(\d+)\S+$/){
if($1 <= 114){ #only investigate up to 114th element
$atomic_num = $1;
}
$elem_set{$atomic_num} = [$a->attr('title'), $a->attr('href')];
}
}
## In each element's page. look for links to other elements in the set
foreach my $elem (keys %elem_set) {
if($elem == 6){
# reconstruct element url to ensure only fetch pages in English
my $elem_url = "http://en.wikipedia.org" . $elem_set{$elem}[1];
$mech->get($elem_url);
#####################################################################
### need help here to exclude links from that mini periodic table ###
#####################################################################
my @target_links = $mech->links();
for my $link ( @target_links ) {
if( $link->url =~ m/^\/(wiki)\/.+$/ && $link->text =~ m/^\w+$/ ){
printf("%s, %s\n", $link->text, $link->url);
}
}
}
}