【问题标题】:Can't locate module(s) using Mojo::DOM无法使用 Mojo::DOM 定位模块
【发布时间】:2015-09-03 08:28:07
【问题描述】:

我是 Mojolicious 的新手。我确信这可能是一个设置问题,但它占用了我一整天的时间。我正在尝试运行这个简单的测试代码

#!/usr/bin/perl

use strict;
use warnings;
use Mojo::DOM;
use Mojo::UserAgent;

my $ua = Mojo::UserAgent->new();

$ua->get('https://stackoverflow.com/questions/26353298/find-links-containing-bold-text-using-wwwmechanize')->res->dom('a div')->ancestors('div.spacer')->each( sub { say $_->all_text } );

我在这个位置找到的

find links containing bold text using WWW::Mechanize

它失败了

Can't locate object method "ancestors" via package "Mojo::Collection" at ./test4.pl line 10.

我已经卸载并重新安装了无数次,尝试了不同的软件包安装选项(cpan、cpanm、直接链接等)。没有骰子。我有点困惑 Mojo::Collection 模块中似乎没有“祖先”方法(继承或没有),但我见过其他几个类似的例子,它们似乎以相同的方式使用相同的方法.不仅仅是“祖先”模块——问题似乎也影响了其他一些方法。

我在 Linux Mint 上使用 perl 5.18.2 和 Mojolicious 包 6.11。

感谢您的帮助。

【问题讨论】:

    标签: perl dom mojolicious mojo


    【解决方案1】:

    像这样调试单个长链语句非常困难,最好将其拆分为单独的步骤

    将参数传递给dom 方法与在DOM 对象上使用该参数调用find 相同。 find 返回一个Mojo::Collection,这是有道理的,因为它是与 CSS 选择器匹配的节点集。 ancestors 方法仅适用于单个节点,因此您必须使用 firstlast 之类的内容选择一个,或者使用 each 一次处理它们

    这是对您的代码的重写,它产生了我认为是预期的结果

    use strict;
    use warnings;
    use 5.010;
    
    use open qw/ :std :encoding(UTF-8) /;
    
    use Mojo;
    
    my $url = 'http://stackoverflow.com/q/26353298';
    
    my $ua = Mojo::UserAgent->new->max_redirects(3);
    my $dom = $ua->get($url)->res->dom;
    
    my $divs = $dom->find('a div');
    
    printf "%d matching div elements:\n\n", $divs->size;
    
    my $n;
    for my $div ( $divs->each ) {
      my $spacers = $div->ancestors('div.spacer');
      for my $spacer ( $spacers->each ) {
        printf "%2d -- %s\n\n", ++$n, $spacer->all_text;
      }
    }
    

    输出

    20 matching div elements:
    
     1 -- Stack Exchange Podcast #65: The Word Has Two Meanings, You See
    
     2 -- PIVOTing into a new career: please welcome Taryn Pratt, bluefooted Community …
    
     3 -- 0 Can't locate module(s) using Mojo::DOM
    
     4 -- 0 tiny runable www::Mechanize examples for the beginner
    
     5 -- 2 Perl WWW::Mechanize and authenticated proxy
    
     6 -- 0 How to process a simple loop in Perl's WWW::Mechanize?
    
     7 -- 1 not able to click a button in www::mechanize perl
    
     8 -- 0 Perl - Mechanize? - How to get all links in a page up to a specific “delimiter” text
    
     9 -- 2 Why am I getting gibberish content using Perl's WWW::Mechanize?
    
    10 -- 1 Getting error in accessing a link using WWW::Mechanize
    
    11 -- 3 Perl WWW::Mechanize Web Spider. How to find all links
    
    12 -- 0 What is the best way to extract unique URLs and related link text via perl mechanize?
    
    13 -- 0 perl WWW::Mechanize file upload error
    

    【讨论】:

    • 谢谢!它现在正在工作。第一个和最后一个节点业务我不了解。现在我明白它正在向我传递数据结构中的一组节点。是的,最初的版本有点太紧凑了(特别是因为我刚刚熟悉了 Mojo)。再次感谢。
    • @JimSimmonds:我很高兴能提供帮助:Mojolicious 是一个不错的框架。我只希望它支持 XPath 表达式以及 CSS 选择器。 firstlast 只是从Mojo::Collection 中提取一个Mojo::DOM 对象的示例。你也可以使用each返回一个节点列表,你可以像数组引用一样索引它,所以$collection->first$collection->[0]等是一样的
    猜你喜欢
    • 2012-12-13
    • 2012-10-28
    • 2012-09-30
    • 2014-07-26
    • 2017-07-23
    • 2017-09-04
    • 2016-01-22
    • 2018-03-30
    • 2017-12-21
    相关资源
    最近更新 更多