[英]Check if an url is blocked by robots.txt using Perl
谁能告诉我示例代码来检查 url 是否已被 robots.txt 阻止? 我们可以在 robots.txt 中指定完整的 url 或目录。 Perl中有没有帮手function?
查看WWW::RobotRules
:
The following methods are provided:
$rules = WWW::RobotRules->new($robot_name)
This is the constructor for WWW::RobotRules objects. The first
argument given to new() is the name of the robot.
$rules->parse($robot_txt_url, $content, $fresh_until)
The parse() method takes as arguments the URL that was used to
retrieve the /robots.txt file, and the contents of the file.
$rules->allowed($uri)
Returns TRUE if this robot is allowed to retrieve this URL.
WWW::RobotRules是标准的 class 用于解析 robots.txt 文件,然后检查 URL 以查看它们是否被阻止。
您可能还对LWP::RobotUA感兴趣,它将其集成到LWP::UserAgent中,根据需要自动获取和检查 robots.txt 文件。
加载 robots.txt 文件并在文件中搜索“Disallow:”。 然后检查以下模式(在 Disallow 之后:)是否在您的 URL 中。 如果是这样,则 URL 被 robots.txt 禁止
示例 - 您在 robots.txt 中找到以下行:
禁止:/cgi-bin/
现在删除“Disallow:”并检查“/cgi-bin/”(剩余部分)是否直接位于 TLD 之后。
如果您的 URL 看起来像:
www.stackoverflow.com/cgi-bin/somwhatelse.pl
它被禁止了。
如果您的 URL 看起来像:
www.stackoverflow.com/somwhatelse.pl
没关系。 您可以在http://www.robotstxt.org/上找到完整的规则集。 如果您因任何原因无法安装其他模块,则可以采用这种方式。
最好使用来自 cpan 的模块:cpan 上有一个很棒的模块可以用来处理它: LWP::RobotUA 。 LWP (libwww) 是 perl 中的网络访问标准恕我直言 - 此模块是其中的一部分,可确保您的行为良好。
WWW::RobotRules 跳过规则“ substring ”
User-agent: *
Disallow: *anytext*
url http://example.com/some_anytext.html被通过(未禁止)
哼,你好像都没看过! 在搜索结果的第一页上,我看到各种自动为您处理 robots.txt 的下载引擎,并且至少有一个完全按照您的要求执行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.