如何使用Mojolicious在段落元素中打印文本

时间:2016-01-11 10:39:09

标签: html perl mojolicious

1

当我运行此代码时,我得到以下输出

  

允许您使用罗马数字编写Perl 6代码:

但我希望输出为:

  

perl6-slang-roman允许您使用罗马数字编写Perl 6代码:

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:2)

  • text方法只会在节点内立即获取文本节点。要获取所有后代文本节点,您需要使用all_text

  • 再次使用join然后split将元素分成列表也相当丑陋。 find方法返回一个Mojo::Collection对象,可以直接编入索引

  • 将所选的div限制为具有所需类别的

  • 也是如此

喜欢这个

use strict;
use warnings;
use feature 'say';

use Mojo;

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

my $collection = $ua->get('http://blogs.perl.org/')->res->dom->find('div.entry-body > p ');

say $collection->[0]->all_text;

输出

perl6-slang-roman lets you write your Perl 6 code using Roman numerals: