2

我正在编写一个脚本来重新排列 html 内容,但遇到了 2 个问题。我有这个 html 结构,它是电影标题和发行年份,缩略图分为 5 列。我想生成新的 html 文件,其中包含从 2011 年到 1911 年按十年分组的电影,例如 present-2011;2010-2001;2000-1991;等等

<table>
    <tr>
      <td class="basic" valign="top">
        <a href="details/267226.html" title="" id="thumbimage">
          <img src="images/267226f.jpg"/>
        </a>
        <br/>Cowboys &amp; Aliens &#160;(2011)
</td>
      <td class="basic" valign="top">
        <a href="details/267185.html" title="" id="thumbimage">
          <img src="images/267185f.jpg"/>
        </a>
        <br/>The Hangover Part II &#160;(2011)
</td>
      <td class="basic" valign="top">
        <a href="details/267138.html" title="" id="thumbimage">
          <img src="images/267138f.jpg"/>
        </a>
        <br/>Friends With Benefits &#160;(2011)
</td>
      <td class="basic" valign="top">
        <a href="details/266870.html" title="" id="thumbimage">
          <img src="images/266870f.jpg"/>
        </a>
        <br/>Beauty And The Beast &#160;(1991)
</td>
      <td class="basic" valign="top">
        <a href="details/266846.html" title="" id="thumbimage">
          <img src="images/266846f.jpg"/>
        </a>
        <br/>The Fox And The Hound &#160;(1981)
</td>
    </tr>


......

</table>

我不知道如何解决的一个问题是,在删除与十年不匹配的电影后,我留下了空的“tr”标签和缩略图位置,并且不知道如何重新排列 5 列中的每一行,其中充满了 5 个标题. 以及如何通过一次脚本调用来处理每个十年。谢谢。

use autodie;
use strict;
use warnings;
use File::Slurp;
use HTML::TreeBuilder;    

my $tree = HTML::TreeBuilder->new_from_file( 'test.html' );

for my $h ( $tree->look_down( class => 'basic' ) ) {

    edit_links( $h );      

    my ($year) = ($h->as_text =~ /.*?\((\d+)\).*/);
    if ($year > 2010 or $year < 2001) {
        $h->detach;
        write_file( "decades/2010-2001.html", \$tree->as_HTML('<>&',' ',{}), "\n" );
    }
}    

sub edit_links {
    my $h = shift;

    for my $link ( $h->find_by_tag_name( 'a' ) ) {
        my $href = '../'.$link->attr( 'href' );
        $link->attr( 'href', $href );
    }

    for my $link ( $h->find_by_tag_name( 'img' ) ) {
        my $src = '../'.$link->attr( 'src' );
        $link->attr( 'src', $src );
    }
}
4

1 回答 1

0

下面的方法应该做你想要的。在 HTML 文件处理期间,%decade设置哈希,每个键是十年的结束年份和适当单元格的值 arrayref。

第二个循环遍历每个十年的散列和输出文件,用<tr>标签包围每 5 个单元格。

use strict;
use HTML::TreeBuilder;
use File::Slurp;
use List::MoreUtils qw(part);

my $tree = HTML::TreeBuilder->new_from_file('test.html');

my %decade = ();

for my $h ( $tree->look_down( class => 'basic' ) ) {

    edit_links( $h );

    my ($year) = ($h->as_text =~ /.*?\((\d+)\).*/);
    my $dec = (int($year/10) + 1)  * 10;

    $decade{$dec} ||= [];
    push @{$decade{$dec}}, $h;
}

for my $dec (sort { $b <=> $a } keys %decade) {
    my $filename = "decades/" . $dec . "-" . ($dec - 9) . ".html";

    my $idx = 0;
    my @items = map { $_->as_HTML('<>&',' ',{}) } @{ $decade{$dec} };
    my $contents = join('',
        '<table>',
        (map { "<tr>@$_</tr>" } part { int($idx++ / 5) } @items),
        '</table>');

    write_file( $filename, $contents);
}

...
于 2011-12-10T19:54:12.373 回答