簡體   English   中英

檢查 url 是否被 robots.txt 使用 Perl 阻止

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM