【问题标题】:How can I extract iframes from text with Perl's Mojo::DOM如何使用 Perl 的 Mojo::DOM 从文本中提取 iframe
【发布时间】:2013-04-24 20:28:25
【问题描述】:

我有this text,当我这样做时:

print STDERR (Mojo::DOM->new($args->{'body'})->at('iframe')); 

输出:

<iframe allowfullscreen="" frameborder="0" height="360" scrolling="no" 
src="http://localhost:8000/embed/static/clips/2012/12/17/28210/test-rush" width="480">
</iframe>`

它只是打印正文中的第一个iframe。为什么它不打印其他 iframe,我可以将所有 iframe 放在一个数组中吗?

【问题讨论】:

  • 根据 Mojo::Dom 文档。 at 函数只找到匹配的 first 元素。所以它应该只返回 1。我认为 find 是你所追求的,因为它返回一个匹配的集合
  • 谢谢,这是正确的答案。请回答问题,以便我投票。

标签: perl mojo


【解决方案1】:

根据Mojo::Dom 文档。 at 函数只查找第一个匹配的元素。所以它应该只返回 1。我认为 find 是你所追求的,因为它返回一个匹配的集合

use strict;
use warnings;

use Mojo::DOM;

my $dom = Mojo::DOM->new();
while (<DATA>) {
    $dom->append_content($_);
}

#print $dom;

print $dom->find('iframe');

__DATA__
<p>No one's telling the truth anymore, and that makes the numbers suspect.</p>
<p><iframe width="480" height="360" src="http://localhost:8000/embed/static/clips/2012/12/17/28210/test-rush" allowfullscreen="" frameborder="0" scrolling="no"></iframe></p>
<p>Instead of addressing the fact that some text</p>
<p><iframe width="480" height="360" src="http://localhost:8000/embed//static/video/2012/09/07/fnc-ff-20120907-doocytaxes" allowfullscreen="" frameborder="0" scrolling="\&quot;no\&quot;"></iframe></p>
<p>The very first example AP cites was already corrected.some text ....Reacting to recent <a href="/blog/2013/04/17/major-errors-undermine-key-argument-for-austeri">research</a> that has questions.</p>
<p><iframe width="480" height="360" src="http://localhost:8000/embed/static/clips/2013/04/29/29939/fnc-an-20130429-hemmermooredebtgdp" allowfullscreen="" frameborder="0" scrolling="no"></iframe></p>
<p>&nbsp;Arriving at such a conclusion requires not only obscuring the importance in pushing global austerity <a href="/static/images/item/gdp-components.jpg">strong measures</a> of too little spending.</p>

打印您的 iframe:

<iframe allowfullscreen="" frameborder="0" height="360" scrolling="no" src="http://localhost:8000/embed/static/clips/2012/12/17/28210/test-rush" width="480"></iframe> <iframe allowfullscreen="" frameborder="0" height="360" scrolling="\&quot;no\&quot;" src="http://localhost:8000/embed//static/video/2012/09/07/fnc-ff-20120907-doocytaxes" width="480"></iframe> <iframe allowfullscreen="" frameborder="0" height="360" scrolling="no" src="http://localhost:8000/embed/static/clips/2013/04/29/29939/fnc-an-20130429-hemmermooredebtgdp" width="480"></iframe>

编辑:

  1. 您可以使用Mojo::Collectioneach 函数遍历每个 iframe:

    我的 $collection = $dom->find('iframe');

    $collection->each(sub {
        my ($e, $count) = @_;
        print "$count: $e\n"; # Or do something besides print. 
     });
    
  2. 你可以添加一个@来像一个数组一样循环它:

    foreach (@$collection) {
       print "\n Next Elt.:", $_->{src}, ",\n"; #still access elements of iframe with ->
    }
    

【讨论】:

  • 您知道如何将每个 iframe 添加到数组中吗?
  • find 函数返回一个Mojo::Collection。它提供了很多有用的功能。您可以使用each 函数遍历每一帧。 search.cpan.org/~sri/Mojolicious-3.97/lib/Mojo/Collection.pm
  • 2. 正是我想要的。你钉了它。感谢所有的帮助,谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-30
  • 1970-01-01
  • 2015-08-18
  • 1970-01-01
  • 2013-06-30
相关资源
最近更新 更多