html - Mojo::DOM HTML 提取

标签 html perl mojo-dom

我正在尝试从结构完美的网页中提取大量数据,并与 Mojo::DOM 作斗争。方法。如果有人能指出我正确的方向,我将非常感激。

带有有趣数据的截断 HTML 如下:

 <div class="post" data-story-id="3964117" data-visited="false">//extracting story-id
  <h2 class="post_title page_title"><a href="http://example.com/story/some_url" class="to-comments">header.</a></h2>
  //useless data and tags

<a href="http://example.com/story/some_url" class="b-story__show-all">
  <span>useless data</span>
</a>

<div class="post_tags">
  <ul>
    <li class="post_tag post_tag_strawberry hidden"><a href="http://example.com/search.php?n=32&r=3">&nbsp;</a></li>
    <li class="post_tag"><a href="http://example.com/tag/tag1/hot">tag1</a></li>
    <li class="post_tag"><a href="http://example.com/tag/tag2/hot">tag2</a></li>
    <li class="post_tag"><a href="http://example.com/tag/tag1/hot">tag3</a></li>
  </ul>
</div>

<div class="post_actions_box">

  <div class="post_rating_box">
    <ul data-story-id="3964117" data-vote="0" data-can-vote="true">
      <li><span class="post_rating post_rating_up control">&nbsp;</span></li>
      <li><span class="post_rating_count control label">1956</span></li> //1956 - interesting value
      <li><span class="post_rating post_rating_down control">&nbsp;</span></li>
    </ul>
  </div>

  <div class="post_more_box">
    <ul>
      <li>
        <span class="post_more control">&nbsp;</span>
      </li>
      <li>
        <a class="post_comments_count label to-comments" href="http://example.com/story/some_url#comments">132&nbsp;<i>&nbsp;</i></a>
      </li>
    </ul>
  </div>

</div>
</div>

我现在拥有的是

use strict;
use warnings;

use Data::Dumper;
use Mojo::DOM;


my $file = "index2.html";
local( $/, *FH ) ;
open( FH, $file ) or die "sudden flaming death\n";
my $text = <FH>;
my $dom = Mojo::DOM->new;
$dom->parse($text);
my $ids = $dom->find('div.post')
    ->each (sub {print $_->attr('data-story-id'), "\n";});
$dom->find('a.to-comments')->each (sub {print $_->text, "\n";});

这个困惑从 src 和 header 值中提取 data-story-id (使用 href 值进行了相同的测试),但我所有其他尝试都失败了。

3964117
Header
132

未提取“post_ rating_count 控制标签”。我可以通过搜索 a.to-comments 并返回 attr('href') 来获取第一个 href 值,但由于某种原因,它也返回链接的值在该段的末尾添加 class="post_comments_count label to-comments"。 header 值提取也会发生同样的情况。

最后,我正在寻找一个具有以下字段的数据结构的数组:

  • story-id(这是成功的)
  • href(不知何故,匹配超出需要的内容。)
  • 标题(不知何故,匹配超出需要的内容。)
  • 字符串形式的标签列表(不知道该怎么做)

而且,我觉得可以优化一下代码,让它看起来更好一点,但我的功夫没那么强。

最佳答案

正如我在评论中所说,您的 HTML 格式错误。我已经猜到失踪的<div>在哪里了可能会去,但我可能错了。我假设最后一个 </div>数据中对应第一个<div> ,这样整个 block 就构成了一个帖子

您遇到的主要问题是尝试在 each 内完成所有操作方法调用您的 Mojo::Collection对象。使用 Perl 迭代每个集合要容易得多,就像这样

use strict;
use warnings;

use Mojo::DOM;

use constant HTML_FILE => 'index2.html';

my $html = do {
    open my $fh, '<', HTML_FILE or die $!;
    local $/;
    <$fh>;
};

my $dom = Mojo::DOM->new($html);

for my $post ( $dom->find('div.post')->each ) {

    printf "Post ID:     %s\n", $post->attr('data-story-id');

    my $anchor = $post->at('h2.post_title > a');
    printf "Post href:   %s\n", $anchor->attr('href');
    printf "Post header: %s\n", $anchor->text;

    my @tags = $post->find('li.post_tag > a')->map('text')->each;

    printf "Tags:        %s\n", join ', ', @tags;

    print "\n";
}

输出

Post ID:     3964117
Post href:   http://example.com/story/some_url
Post header: Header
Tags:        some_value, tag1, tag2, tag3

关于html - Mojo::DOM HTML 提取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35171586/

相关文章:

javascript - jquery 点击 li 到下拉列表不起作用

html - 隐藏宽度大于正文的图像溢出

regex - 在 awk 中使用 Perl 正则表达式属性

jquery - F5(刷新)充当提交

perl - 使用 Perl 抓取 HTML 文件,仅返回内容,按顺序

html - 使用 CSS3 嗅探文档类型,特别是使用 Mojo::DOM

html - 如何创建自定义 HTML 元素

javascript - 充满时使居中的 DIV 滚动

perl - 为什么一个模块可以自行编译,但在其他地方使用时会失败?

html - 使用 Mojo::DOM 选择 CSS