www :: Mechanize :: Firefox collow_link不起作用



我正在尝试按照perl中的链接进行链接。我的初始代码:

use WWW::Mechanize::Firefox;
use Crypt::SSLeay;
use HTML::TagParser;
use URI::Fetch;
$ENV{PERL_LWP_SSL_VERIFY_HOSTNAME}=0; #not verifying certificate
my $url = 'https://';
$url = $url.@ARGV[0]; 
my $mech = WWW::Mechanize::Firefox->new;
$mech->get($url);
$mech->follow_link(tag => 'a', text => '<span class="normalNode">VSCs</span>');
$mech->reload();

我在这里发现 tag text 选项以这种方式工作,但我得到了错误 mozrepl :: remoteObject:remoteObject:syntaxerror:表达式不是法律表达式。我试图逃脱文本中的一些字符,但是错误仍然相同。然后我更改了代码添加:

my @list = $mech->find_all_links();
my $found = 0;
my $i=0;
while($i<=$#list && $found == 0){
    print @list[$i]->url()."n";
    if(@list[$i]->text() =~ /VSCs/){
    print @list[$i]->text()."n";
    my $follow =@list[$i]->url();
    $mech->follow_link( url => $follow);
}
    $i++;
}

但再一次有一个错误:找不到匹配'//a [(@href =" https://... ),还有许多似乎是链接的文本描述。我希望我能使自己明确,如果没有,请告诉我还要添加什么。感谢所有的帮助。

这是我要遵循的链接的部分是:

<li id="1" class="liClosed"><span class="bullet clickable">&#160;</span><b><a href="/centcfg/vsc_list.asp?entity=allvsc&amp;selector=All"><span class="normalNode">VSCs</span></a></b>
      <ul id="1.l1">
        <li id="i1.i1" class="liBullet"><span class="bullet">&#160;</span><b><a href="/centcfg/vsc_edit.asp?entity=vsc&amp;selector=1"><span class="normalNode">First</span></a></b></li>
        <li id="i1.i2" class="liBullet"><span class="bullet">&#160;</span><b><a href="/centcfg/vsc_edit.asp?entity=vsc&amp;selector=2"><span class="normalNode">Second</span></a></b></li>
        <li id="i1.i3" class="liBullet"><span class="bullet">&#160;</span><b><a href="/centcfg/vsc_edit.asp?entity=vsc&amp;selector=3"><span class="normalNode">Third</span></a></b></li>
        <li id="i1.i4" class="liBullet"><span class="bullet">&#160;</span><b><a href="/centcfg/vsc_edit.asp?entity=vsc&amp;selector=4"><span class="normalNode">Fourth</span></a></b></li>
        <li id="i1.i5" class="liBullet"><span class="bullet">&#160;</span><b><a href="/centcfg/vsc_edit.asp?entity=vsc&amp;selector=5"><span class="normalNode">None</span></a></b></li>
</ul>

我在Windows 7中工作,Mozrepl是1.1版,我正在使用草莓Perl 5.16.2.1,用于64位

用给定代码戳戳后,我能够使w :: m :: f以以下方式遵循链接:

use WWW::Mechanize::Firefox;
use Crypt::SSLeay;
use HTML::TagParser;
use URI::Fetch;
...
$mech->follow_link(xpath => '//a[text() = "<span class="normalNode">VSCs</span>"]');
$mech->reload();

注意给出的xpath参数而不是text

我没有花很长时间查看w :: m :: f来源,但是在引擎盖下,它试图将给定的 text参数转换为xpath字符串,如果 text包含xml/html的数量,这是您的案例,这可能会让他发疯。

我建议您尝试:

$mech->follow_link( url_regex => qr/selector=All/ );

最新更新