如何使用html::TreeBuilder解析html



这是我想要解析的代码

[...]
<div class="item" style="clear:left;">
 <div class="icon" style="background-image:url(http://nwn2db.com/assets/builder/icons/40x40/is_acidsplash.png);">
 </div>
  <h2>Acid Splash</h2>
   <p>Caster Level(s): Wizard / Sorcerer 0
   <br />Innate Level: 0
   <br />School: Conjuration
   <br />Descriptor(s): Acid
   <br />Component(s): Verbal, Somatic
   <br />Range: Medium
   <br />Area of Effect / Target: Single
   <br />Duration: Instant
   <br />Save: None
   <br />Spell Resistance: Yes
   <p>
   You fire a small orb of acid at the target for 1d3 points of acid damage.
 </div>
[...]

这是我的算法:

my $text = '';
scan_child($spells);
print $text, "n";
sub scan_child {
  my $element = $_[0];
  return if ($element->tag eq 'script' or
             $element->tag eq 'a');   # prune!
  foreach my $child ($element->content_list) {
    if (ref $child) {  # it's an element
      scan_child($child);  # recurse!
    } else {           # it's a text node!
      $child =~ s/(.*):/\item [$1]/; #itemize
      $text .= $child;
      $text .= "n";
    }
   }
  return;
}

它获取模式<key> : <value>并修剪像<script><a>...</a>这样的垃圾。我想改进它,以便获得<h2>...</h2>报头和所有<p>...<p>块,这样我就可以添加一些LaTeX标签。

有线索吗?

提前谢谢。

因为这可能是XY问题。。。

Mojo::DOM是一个更现代的框架,用于使用css选择器解析HTML。下面从文档中提取您想要的P元素:

use strict;
use warnings;
use Mojo::DOM;
my $dom = Mojo::DOM->new(do {local $/; <DATA>});
for my $h2 ($dom->find('h2')->each) {
    next unless $h2->all_text eq 'Acid Splash';
    # Get following P
    my $next_p = $h2;
    while ($next_p = $next_p->next_sibling()) {
        last if $next_p->node eq 'tag' and $next_p->type eq 'p';
    }
    print $next_p;
}
__DATA__
<html>
<body>
<div class="item" style="clear:left;">
 <div class="icon" style="background-image:url(http://nwn2db.com/assets/builder/icons/40x40/is_acidsplash.png);">
 </div>
  <h2>Acid Splash</h2>
   <p>Caster Level(s): Wizard / Sorcerer 0
   <br />Innate Level: 0
   <br />School: Conjuration
   <br />Descriptor(s): Acid
   <br />Component(s): Verbal, Somatic
   <br />Range: Medium
   <br />Area of Effect / Target: Single
   <br />Duration: Instant
   <br />Save: None
   <br />Spell Resistance: Yes
   <p>
   You fire a small orb of acid at the target for 1d3 points of acid damage.
 </div>
 </body>
 </html>

输出:

<p>Caster Level(s): Wizard / Sorcerer 0
   <br>Innate Level: 0
   <br>School: Conjuration
   <br>Descriptor(s): Acid
   <br>Component(s): Verbal, Somatic
   <br>Range: Medium
   <br>Area of Effect / Target: Single
   <br>Duration: Instant
   <br>Save: None
   <br>Spell Resistance: Yes
   </p>

我使用look_down()方法扫描HTML。使用look_down(),我可以首先返回class="item"的所有div的列表。

然后我可以对它们进行迭代,找到并处理h2p,然后使用//作为拆分器对它们进行拆分。

相关内容

  • 没有找到相关文章

最新更新