- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试编写一个最小的网络爬虫。目的是从种子中发现新的 URL 并进一步抓取这些新的 URL。代码如下:
use strict;
use warnings;
use Carp;
use Data::Dumper;
use WWW::Mechanize;
my $url = "http://foobar.com"; # example
my %links;
my $mech = WWW::Mechanize->new(autocheck => 1);
$mech->get($url);
my @cr_fronteir = $mech->find_all_links();
foreach my $links (@cr_fronteir) {
if ( $links->[0] =~ m/^http/xms ) {
$links{$links->[0]} = $links->[1];
}
}
我被困在这里,如何进一步爬取 %links 中的链接,以及如何增加深度以防止溢出。欢迎提出建议。
最佳答案
Mojolicious web 框架提供了一些对网络爬虫有用的有趣功能:
fork()
开销)这是一个递归抓取本地 Apache 文档并显示页面标题和提取链接的示例。它使用 4 个并行连接并且不超过 3 个路径级别,每个提取的链接只访问一次:
#!/usr/bin/env perl
use 5.010;
use open qw(:locale);
use strict;
use utf8;
use warnings qw(all);
use Mojo::UserAgent;
# FIFO queue
my @urls = (Mojo::URL->new('http://localhost/manual/'));
# User agent following up to 5 redirects
my $ua = Mojo::UserAgent->new(max_redirects => 5);
# Track accessed URLs
my %uniq;
my $active = 0;
sub parse {
my ($tx) = @_;
# Request URL
my $url = $tx->req->url;
say "\n$url";
say $tx->res->dom->at('html title')->text;
# Extract and enqueue URLs
for my $e ($tx->res->dom('a[href]')->each) {
# Validate href attribute
my $link = Mojo::URL->new($e->{href});
next if 'Mojo::URL' ne ref $link;
# "normalize" link
$link = $link->to_abs($tx->req->url)->fragment(undef);
next unless $link->protocol =~ /^https?$/x;
# Don't go deeper than /a/b/c
next if @{$link->path->parts} > 3;
# Access every link only once
next if ++$uniq{$link->to_string} > 1;
# Don't visit other hosts
next if $link->host ne $url->host;
push @urls, $link;
say " -> $link";
}
return;
}
sub get_callback {
my (undef, $tx) = @_;
# Parse only OK HTML responses
$tx->res->code == 200
and
$tx->res->headers->content_type =~ m{^text/html\b}ix
and
parse($tx);
# Deactivate
--$active;
return;
}
Mojo::IOLoop->recurring(
0 => sub {
# Keep up to 4 parallel crawlers sharing the same user agent
for ($active .. 4 - 1) {
# Dequeue or halt if there are no active crawlers anymore
return ($active or Mojo::IOLoop->stop)
unless my $url = shift @urls;
# Fetch non-blocking just by adding
# a callback and marking as active
++$active;
$ua->get($url => \&get_callback);
}
}
);
# Start event loop if necessary
Mojo::IOLoop->start unless Mojo::IOLoop->is_running;
有关更多网络抓取提示和技巧,请阅读 I Don’t Need No Stinking API: Web Scraping For Fun and Profit文章。
关于perl - 递归网络爬虫 perl,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13899872/
我在一个网站上工作。 我需要了解我的网站是否获得了来自 Google 或任何其他搜索引擎的抓取工具/机器人的访问 在我的应用程序中,我正在拦截 http 请求。并且需要查明爬虫/机器人是否正在发出 h
我与北京理工大学python爬虫的邂逅 在人生的旅途中,我们时常有机会邂逅一些惊艳的事物,它们仿佛一束亮光照亮了前行的道路。而我,一个平凡的人类,也不例外。回想起那个让我心动的时刻,我想起了与北京理工
从“爬虫”谈起 在这个信息爆炸的时代,互联网上的数据宛如茫茫星海,琳琅满目。但是,我们能否想象到这些数据背后隐藏着多少珍贵的信息呢?有人说,要想获得这些数据,我们需要借助一种神奇的力量——爬虫。 什么
探索未知的领域 曾几何时,我陷入了对互联网世界的好奇之中。作为一个普通的人类,我对于计算机技术的了解可谓是一窍不通。然而,好奇心驱使着我踏上了学习的征途。 奇妙的爬虫 在探索计算机领域的过程中,我翻阅
教室的奇妙之旅 在一个晴朗的早晨,我来到了一所神奇且富有魅力的教室。这里充满了未知的冒险和无限的学习可能。嘿,你没听错,就是那个零基础Python视频教程课程的教室!让我带你一起踏上这趟令人期待的爬虫
零基础学python3 爬虫 嗨,各位小伙伴们!今天要和大家分享一段有关学习Python3爬虫的故事。无论是技术小白还是编程高手,都可以从中获益良多。 1. 邂逅神奇的爬虫世界 就像是迷失在未知之地的
我正在开发一个 Python(3) 程序,在该程序中,我必须使用输入作为表示各种操作的多次迭代列表来返回目录迭代的移动次数,例如: ../ 表示移动到当前文件夹的父文件夹。 ./ 保留在同一文件夹中
我和Python:一段关于爬虫和第三方库的故事 曾经有一个人,他和一门编程语言结下了不解之缘。这门语言就是Python,一个灵活而强大的工具,让他的世界变得多姿多彩。 遇见爬虫:探索未知的世界 某天,
偷心技艺:大众点评TOP10 爬虫 Python 故事发生在一个被互联网包围的城市,充满了各种各样的餐馆、咖啡厅和美食街巷。每天都有无数的食客们纠结于选择哪家餐厅才能获得满足和享受。就在这个时候,我偶
登山寻宝:为什么Python爬虫部分网页源码 人生就像登山寻宝的旅程,充满了未知和挑战。而对于爱好编程的我来说,写一个Python爬虫,探索网络世界也是一种刺激的冒险。今天,我想和大家分享一下为什么要
插班生的悲喜交加 记得那个夏天,我踏入了编程的殿堂,成为了一名程序员。对于一个毫无基础的人来说,这是一次勇敢而激动的尝试。然而,更令我兴奋的是,在我的码农之旅中,我发现了一种神奇的力量——Python
一、 意外的宝藏 当我踏进了编程的大门,发现了那个绚丽多彩的世界时,我仿佛捡到了一个意外的宝藏。在编码的世界里,我像是一个魔术师,用着神奇的符号与逻辑来创造奇迹。然而,随着时间的推移,我渐渐找到了一种
“呜呜呜~” 夏日的阳光穿过微风,洒在我身上,仿佛一片温暖而宁静的海洋。我望着眼前充满希望与挑战的屏幕,满脸的期待和困惑。作为一个刚刚入门的编程初学者,我竭尽全力探索着那座神秘的编程世界,好像是航行在
我的编程之旅 嗨,大家好!我是一个普通的人类,对于编程世界充满着无限的好奇和热情。今天,我想向大家讲述一段关于我的编程经历,特别是在爬虫领域的探索之旅。 1. 踏上未知的征途 就像一只踏上大海的小船,
揭秘神奇的爬虫世界:探寻京东销量数据库 从古至今,人类一直渴望了解世界的各个角落。而对于互联网这个充满未知和神秘的领域来说,我们更是怀着好奇和充满冒险精神的心态去探索。今天,我将带领大家踏上一段激动人
啊!你好呀!很高兴能遇到你,让我向你讲一个关于python爬虫爬取关键词排名的故事。 章节一:奇妙的网络世界 网络就像一片浩瀚的海洋,里面有无数宝藏等待着我们发掘。而当我们学会了使用python这把钥
一次意外的奇遇 在我们生活的世界中,技术如今已经无处不在,无论是大型软件开发还是个人应用,都离不开它的支持。而其中最为神奇的领域之一,就是爬虫技术。想象一下,你有可能仅凭几行代码,就能够在电脑屏幕上将
奇遇之旅 在这个大千世界中,我有幸踏上了一次令人兴奋的冒险之旅。故事的开始,是一个寂静而神秘的夜晚。 迷宫的入口 深夜的城市,街道上弥漫着微弱的灯光,仿佛点缀着无数的星星。我沿着曲径通幽的小巷走着,脚
python 爬虫 自动上传下载 从前有一个热爱编程的小伙子,他喜欢利用自己的技术解决各种实际问题。有一天,他面对一个任务:需要在网络上批量上传和下载文件。这可不是一件轻松的工作,但对于这位小伙子来说
一场奇妙的探险之旅:Python编程语言中的爬虫第三方库 故事从一个寻宝者开始。他身穿一件斑驳的皮衣,手持一柄锈迹斑斑的剑,如同一个当代版的荒野游侠。这位勇士的目标是探索Python编程语言中的神秘领
我是一名优秀的程序员,十分优秀!