首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Mojo::DOM提取

Mojo::DOM提取
EN

Stack Overflow用户
提问于 2016-02-03 08:08:06
回答 1查看 923关注 0票数 2

我正试图从一个结构完美的网页中提取相当多的数据,并在Mojo::DOM方法上苦苦挣扎。如果有人能给我指明正确的方向,我会非常感激的。

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

代码语言:javascript
复制
 <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>

我现在拥有的是

代码语言:javascript
复制
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和头值中提取data-story-id (使用href值进行了同样的测试),但是所有其他尝试都失败了。

代码语言:javascript
复制
3964117
Header
132

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

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

  • 故事-id(这是一个成功)
  • href (不知怎么地,匹配得比需要的多)。
  • 标头(不知怎么地,匹配的比需要的要多)。
  • 作为字符串的标记列表(不知道如何做到这一点)

更重要的是,我觉得可以优化代码,让它看起来更好一点,但我的功夫并不强。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-02-03 09:42:55

正如我在评论中所说,您的HTML格式不正确。我已经猜到了失踪的<div>可能会去哪里,但我可能错了。我假设数据中的最后一个</div>对应于第一个<div>,因此整个块构成一个post

您所遇到的主要问题是如何在each方法调用Mojo::Collection对象中完成所有工作。使用Perl来迭代每个集合要容易得多,如下所示

代码语言:javascript
复制
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";
}

输出

代码语言:javascript
复制
Post ID:     3964117
Post href:   http://example.com/story/some_url
Post header: Header
Tags:        some_value, tag1, tag2, tag3
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35171586

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档