我目前正在尝试使用WWW :: Mechanize创建一个Perl webspider.
我想要做的是创建一个webspider,它将抓取URL的整个站点(由用户输入)并从站点的每个页面中提取所有链接.
到目前为止我所拥有的:
use strict; use WWW::Mechanize; my $mech = WWW::Mechanize->new(); my $urlToSpider = $ARGV[0]; $mech->get($urlToSpider); print "\nThe url that will be spidered is $urlToSpider\n"; print "\nThe links found on the url's starting page\n"; my @foundLinks = $mech->find_all_links(); foreach my $linkList(@foundLinks) { unless ($linkList->[0] =~ /^http?:\/\//i || $linkList->[0] =~ /^https?:\/\//i) { $linkList->[0] = "$urlToSpider" . $linkList->[0]; } print "$linkList->[0]"; print "\n"; }
它能做什么:
2.如果找到的链接是/ contact-us或/ help格式,它会在其前面添加“http://www.thestartingurl.com”,以便它变为’http://www.thestartingurl.com/contact -我们’.
问题:
目前它还找到了我不希望它做的外部网站的链接,例如,如果我想蜘蛛’http://www.tree.com’它会找到像http://www.tree.com/find-us这样的链接.
但它也会找到其他网站的链接,如http://www.hotwire.com.
如何阻止它找到这些外部网址?
找到页面上的所有网址后,我还想将这个新的内部链接列表保存到名为@internalLinks的新数组中,但似乎无法使其正常工作.
非常感谢任何帮助,在此先感谢.