提取html中所有的链接,从HTML页面中提取所有链接,从特定表中排除链接

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

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 in this

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

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值