0

HTML 有很多不同的解析器,很难选择正确的解析器。

我的任务是读取url并找到<table>具有特定id的内容,然后解析<tr>该表的所有行以获取内容(文本),以及标签内的<a>链接和<img>图像。<td>

我还需要检查每个行元素的以将数据分类到类别。

我最好的选择是什么,我应该使用哪个库和什么方法来快速获得结果?


我要解析的部分 HTML 代码示例:

<table id="t1">
  <tr class="r1">
    <td class="c1"><a href="..."><img height="50" src="..." width="50" /></a></td>
    <td class="c2">
      <div class="d1">
        <ul class="u1">
          <li class="l1"><a href="..." rel='...'>text here</a></li>
          <li class="l2"><a href="..." rel='...'>text here</a></li>
        </ul>
      </div>
      <div class="d2">
        <a href="...">text here</a>
      </div>
    </td>
    <td class="c3">
      <div ...>...</div>
      <div class="d2">
        <a href="...">text here</a>
      </div>
    </td>
    <td class="c4">text here</td>
    <td class="c5">text here</td>
  </tr>
  ...
</table>
4

1 回答 1

1

使用Web::Query。使用它的方法findandtextattr

use List::Gen qw(mapn);
use Web::Query 'wq';

sub classify {
    my ($l) = @_; my %r;
    mapn { push @{ $r{$_[0]} }, $_[1] } 2, @$l; return %r;
};

my $w = wq('file:///tmp/so11301348.html');
my %rows = classify $w
    # find a <table> with specific id
    ->find('table#t1')
    # parse all <tr> rows of this table for content (text)
    # check class for each row element to sort data to categories
    ->find('tr')->map(sub {
        my (undef, $tr) = @_;
        return $tr->attr('class') => $tr->text;
    });
# (
#     '' => [
#         ' ... '
#     ],
#     r1 => [
#         'text heretext heretext here...text heretext heretext here'
#     ]
# )

my $links_images = $w
# but also <a> links and <img> images within <td> tags
->find('td a, td img')
->map(sub {
    my (undef, $e) = @_;
    return $e->attr('src')
        ? [img => $e->attr('src') => $e->attr('alt')]
        : [a => $e->attr('href') => $e->text];
});
# [
#     ['a',   '...', ''],
#     ['img', '...', ''],
#     ['a',   '...', 'text here'],
#     ['a',   '...', 'text here'],
#     ['a',   '...', 'text here'],
#     ['a',   '...', 'text here']
# ]
于 2012-07-02T22:07:08.820 回答