簡體   English   中英

如何使用WWW :: Mechanize獲得與正則表達式匹配的鏈接?

[英]How can I get links that match a regex using WWW::Mechanize?

我正在嘗試使用正則表達式來捕獲鏈接,但是不能。 我擁有所有鏈接,但是有許多不需要的鏈接。

我要做的是獲取所有鏈接: http://valeptr.com/scripts/runner.php?IM= : http://valeptr.com/scripts/runner.php?IM=遵守此模式。

我把我正在做的腳本:

use warnings;
use strict;
use WWW::Mechanize;
use WWW::Mechanize::Sleepy;

my $Explorador =

    WWW::Mechanize->new(

       agent =>
             'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4) Gecko/20030624',

       sleep => '5..20'
    );

#Proceed to access the URL to find all the links in emails
$Explorador->get("file:/home/alejandro/Escritorio/hehe.php.html");

#If you want debug DOM Document.
#print $Explorador->content();

my @links = $Explorador->links;

foreach my $link (@links) {

   # Retrieve the link URL like:
   # http://valeptr.com/scripts/runner.php?IM=0cdb7d48110375.
   my $href = $link->url;

   foreach my $s ($href) { #Aqui la expresión regular

       my @links = $s =~ qr{
                               (
                               [^B]*
                               )
                               $
                           }x;
       foreach (@links) {
           print "\n",$_;
       }
   }
} 

PS:我猜想這個正則表達式將比看到的更多但看不見。 如果是這樣的話,回來再發表一個相同的帖子。

問題:有大量的鏈接,我需要鏈接與老板終止的鏈接: Http: // valeptr.com/scripts/runner.php?IM=為此,在第19行中,我必須應用表達式Http: // valeptr.com/scripts/runner.php?IM= 這個變量我的@ links = $ Explorador-> links; 他返回所有存在的鏈接。 但是我只希望我上面已經提到的鏈接。 真誠的

為什么不讓WWW::Mechanize為您完成工作,特別是當它可以通過提供的正則表達式為您過濾鏈接時?

my @wanted_links = $Explorador->find_all_links ( 
                                     url_regex => qr{scripts/runner\.php\?IM=}
                                );

沒有for循環!

由於您的參考鏈接似乎是固定的,因此您可以考慮使用substr而不是regex

$ref_link = q!http://valeptr.com/scripts/runner.php?IM=!;
foreach my $link ( $Explorador->links ) {
    my $href = $link->url;
    if ( substr($href, 0, length($ref_link)) eq $ref_link ) {
        push @save, $href;
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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