如何使用Perl的Mojo :: DOM从文本中提取iframe [英] How can I extract iframes from text with Perl's Mojo::DOM

查看:124
本文介绍了如何使用Perl的Mojo :: DOM从文本中提取iframe的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此文本,当我这样做时:

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放在一个数组中吗?

It is just printing the first iframe in the body. Why is it not printing the other iframes and can I put all iframes in an array?

推荐答案

根据Mojo::Dom文档. at函数仅找到第一个匹配的元素.因此,它应该仅返回1.我认为find是您所追求的,因为它返回匹配的集合

According to Mojo::Dom documentation. The at function only finds the first element matching. So it should only return 1. I think find is what you are after, as it returns a collection that matches

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:

prints your iframes:

<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:

  1. You can iterate over each iframe with each function of Mojo::Collection:

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

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

$collection->each(sub {
    my ($e, $count) = @_;
    print "$count: $e\n"; # Or do something besides print. 
 });

  • 您可以添加一个@以像数组一样在其上循环:

  • You can add an @ to loop over it like an array:

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

  • 这篇关于如何使用Perl的Mojo :: DOM从文本中提取iframe的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆