【问题标题】:How do I retrieve a value from nested divs tags with Perl's HTML::TokeParser?如何使用 Perl 的 HTML::TokeParser 从嵌套的 divs 标签中检索值?
【发布时间】:2010-09-16 10:39:04
【问题描述】:

我正在使用 HTML::TokeParser 解析 HTML 文件并获取 div 标记内的数据。我的 HTML 如下:

<div class='t_and_h f_t' id='t_f_i'>
  <div class='icon'>
    <img alt="icon" src=""/>
  </div>
  <div class='t'>
    12:31 PM
  </div>
  <div class='h'>
    <a>Residue 4</a>
  </div>
  <div class='f_t'>
    TRUE
  </div>
</div>

我的代码如下:

while ( $tag = $stream->get_tag('div') ) 
{
    if($tag->[1]{class} eq 't')
    {   
        $time = $stream->get_trimmed_text('</div>');
        print "$time \n";
    }
}

但输出会打印所有字段,即

12:31PM Residue 4 TRUE

我做错了什么?

【问题讨论】:

  • 向我们展示完整的程序,以便我们知道在那段代码之前发生了什么。 :)

标签: html perl


【解决方案1】:

&lt;/div&gt;外部 结束标记。只需将其保留在当前流位置即可,因此:

my $time = $stream->get_trimmed_text; 
# 12:31 PM

【讨论】:

    猜你喜欢
    • 2014-01-14
    • 2012-05-26
    • 2017-03-19
    • 1970-01-01
    • 2018-09-11
    • 1970-01-01
    • 1970-01-01
    • 2019-08-09
    • 1970-01-01
    相关资源
    最近更新 更多