1

我对 Perl/HTML 很陌生。这是我要对WWW::MechanizeHTML::TreeBuilder做的事情:

对于 Wikipedia 上的每个化学元素页面,我需要提取指向 wiki 上其他化学元素页面的所有超链接,并以这种格式打印每个唯一对:

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);
      }
    }

  }
}
4

1 回答 1

2

在找到链接之前,使用 WWW::Mechanize 的update_html方法删除该表。此方法允许您对$mech->content.

于 2010-09-13T21:06:41.163 回答