【问题标题】:Spider a website and retrieve all links that contain a keyword爬取网站并检索包含关键字的所有链接
【发布时间】:2014-12-15 20:22:17
【问题描述】:

如何制作一个复制所有链接的 Bash 脚本(非下载网站)。该功能只是获取所有链接,然后将其保存在txt文件中。

我试过这段代码:

wget --spider --force-html -r -l1 http://somesite.com | grep 'Saving to:'

示例:网站中有下载链接(例如dlink.com),所以我只想复制所有包含dlink.com的单词并保存到txt文件中。

我用谷歌搜索了一遍,发现没有一个有用。

【问题讨论】:

标签: bash copy wget


【解决方案1】:

Perl 中使用适当的解析器:

#!/usr/bin/env perl -w

use strict;
use LWP::UserAgent;
use HTML::LinkExtor;
use URI::URL;

my $ua = LWP::UserAgent->new;
my ($url, $f, $p, $res);

if(@ARGV) { 
    $url = $ARGV[0]; }
else {
    print "Enter an URL : ";
    $url = <>;
    chomp($url);
}

my @array = ();
sub callback {
   my($tag, %attr) = @_;
   return if $tag ne 'a';  # we only look closer at <a href ...>
   push(@array, values %attr) if $attr{href} =~ /dlink\.com/i;
}

# Make the parser.  Unfortunately, we don’t know the base yet
# (it might be diffent from $url)
$p = HTML::LinkExtor->new(\&callback);

# Request document and parse it as it arrives
$res = $ua->request(HTTP::Request->new(GET => $url),
                    sub {$p->parse($_[0])});

# Expand all URLs to absolute ones
my $base = $res->base;
@array = map { $_ = url($_, $base)->abs; } @array;

# Print them out
print join("\n", @array), "\n";

【讨论】:

  • 您好,感谢您的回复,perl 代码在单个链接中完美运行,有没有办法让它递归,爬取所有页面?
  • 一些美元或欧元,我可以做到,是的 =)
  • 给我发电子邮件,如果合理的话,我可能会做一些
猜你喜欢
  • 1970-01-01
  • 2012-07-21
  • 2012-04-23
  • 2016-06-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-23
相关资源
最近更新 更多