Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Perl 在使用Mojo::DOM时无法精确定位子元素_Perl_Mojolicious - Fatal编程技术网

Perl 在使用Mojo::DOM时无法精确定位子元素

Perl 在使用Mojo::DOM时无法精确定位子元素,perl,mojolicious,Perl,Mojolicious,我正在尝试使用和从一个旧的vBulletin论坛中提取文本 vBulletin不使用HTML和CSS进行语义标记,我在使用Mojo::DOM->children获取某些元素时遇到了问题 这些vBulletin帖子的结构因内容而异 单一信息: <div id="postid_12345">The quick brown fox jumps over the lazy dog.<div> 使用$dom->at($div_id)->all_text可以让我在一行完整地看到所有

我正在尝试使用和从一个旧的vBulletin论坛中提取文本

vBulletin不使用HTML和CSS进行语义标记,我在使用
Mojo::DOM->children
获取某些元素时遇到了问题

这些vBulletin帖子的结构因内容而异

单一信息:

<div id="postid_12345">The quick brown fox jumps over the lazy dog.<div>
使用
$dom->at($div_id)->all_text
可以让我在一行完整地看到所有内容,这使得我很难分辨文章中引用的内容和原文

使用
$dom->at($div_id)->text
跳过所有子元素,因此不会拾取引用的文本和扰流板

我首先尝试了
$dom->at($div_id)->children('div')->first
,但这提供了一切,包括HTML

理想情况下,我希望能够在每个帖子中提取所有文本,每个子元素都在自己的行中,例如

 POSTID12345:
 + Quote originally posted by Bob
 + Everyone knows the sky is blue. 
 I disagree with you, Bob. It's obviously green. 
我对Mojo还不熟悉,对Perl还有些生疏。我想自己解决这个问题,但在查阅了文档并摆弄了几个小时后,我的大脑混乱不堪,不知所措。我只是不明白
Mojo::DOM
Mojo::Collections
是如何工作的


任何帮助都将不胜感激

查看Mojo::DOM的源代码,基本上是递归地遍历DOM并提取所有文本。使用该源代码编写自己的DOM函数。它的递归函数依赖于返回单个字符串,在您的字符串中,您可以让它返回一个包含您需要的任何上下文的数组

编辑:


在对IRC进行了一些讨论之后,web抓取示例已经更新,它可能会帮助您进行指导

有一个模块可以修饰HTML树。 对修饰HTML树的目的的解释有点冗长乏味,因此下面的图片显示了该工具的输出,与该模块绑定:

如您所见,HTML树节点变成了单键/值列表,其中键是该节点的XPath,值是该节点的文本属性。 在几个按键中,这就是如何使用HTML::Linear:

#!/usr/bin/env perl
use strict;
use utf8;
use warnings;

use Data::Printer;
use HTML::Linear;

my $hl = HTML::Linear->new;
$hl->parse_file(q(vboard.html));

for my $el ($hl->as_list) {
    my $hash = $el->as_hash;
    next unless keys %{$hash};
    p $hash;
}

+在过去的几天里,你的回答为我指明了正确的方向。非常感谢。
<div id="postid_12345">
    <div>
    <table>
        <tr>
            <td>
            <div>Quote originally posted by Fred</div>
            <div class="spoiler">Yoda is Luke's father!</div>
            </td>
        </tr>
    </table>
    </div>
    <div class="spoiler">No waaaaay!</div>
</div>
for (@post_ids) {
    $mech->get($full_url_of_specific_forum_post);
    my $dom = Mojo::DOM->new($mech->content);
    my $div_id = 'postid_' . $_;

    say $dom->at($div_id)->children('div')->first;
    say $dom->at($div_id)->text;
}
 POSTID12345:
 + Quote originally posted by Bob
 + Everyone knows the sky is blue. 
 I disagree with you, Bob. It's obviously green. 
#!/usr/bin/env perl
use strict;
use utf8;
use warnings;

use Data::Printer;
use HTML::Linear;

my $hl = HTML::Linear->new;
$hl->parse_file(q(vboard.html));

for my $el ($hl->as_list) {
    my $hash = $el->as_hash;
    next unless keys %{$hash};
    p $hash;
}