如何在使用 HTML::Tree 解析后保留标记为 UTF-8 的数据?

发布于 2024-12-02 02:17:36 字数 1303 浏览 3 评论 0原文

我编写了一个脚本,在其中读取 UTF-8 编码的 HTML 文件,然后使用 HTML::Tree。问题是解析后字符串不再标记为 UTF-8。

由于 _utf8_on() 不推荐设置标志的方法,我正在寻找正确的方法。

我的简化代码示例:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use utf8::all;
use autodie;
use HTML::Tree;
use Encode qw/is_utf8/;

my $file = shift;
my $tree;

if ($file) {
    my $content = slurp_in( 'file' => $file );
    $tree = html_tree('content' => $content);
} else {
    die "no file";
}

my $title = $tree->look_down(_tag => 'title');
$title = $title->as_HTML('');

if ( is_utf8( $title ) ) {
    say "OK: $title";
} else {
    say "NOT OK: $title";
}

## SUBS
##
sub slurp_in {
    my %v = @_;

    open(my $fh, "<:utf8", $v{file}) || die "no $v{file}: $!";
    local $/;
    my $content = (<$fh>);
    close $fh;

    if ($content) {
        return $content;
    } else {
        die "no content in $v{file} !";
    }
}

sub html_tree {
    my %v = @_;
    my $tree = HTML::Tree->new();
    $tree->utf8_mode(1); ## wrong call here, no such method, but no warnings on it!
    $tree->parse( $v{content} );

    if ($tree) {
        return $tree;
    } else {
        die "no tree here";
    }
}

I wrote a script, where i slurp in UTF-8 encoded HTML-file and then parse it to tree with HTML::Tree. Problem is that after parsing the strings are not marked as UTF-8 anymore.

As _utf8_on() is not recommended way to set flag on, i am looking for proper way.

My simplified code-example:

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;
use utf8::all;
use autodie;
use HTML::Tree;
use Encode qw/is_utf8/;

my $file = shift;
my $tree;

if ($file) {
    my $content = slurp_in( 'file' => $file );
    $tree = html_tree('content' => $content);
} else {
    die "no file";
}

my $title = $tree->look_down(_tag => 'title');
$title = $title->as_HTML('');

if ( is_utf8( $title ) ) {
    say "OK: $title";
} else {
    say "NOT OK: $title";
}

## SUBS
##
sub slurp_in {
    my %v = @_;

    open(my $fh, "<:utf8", $v{file}) || die "no $v{file}: $!";
    local $/;
    my $content = (<$fh>);
    close $fh;

    if ($content) {
        return $content;
    } else {
        die "no content in $v{file} !";
    }
}

sub html_tree {
    my %v = @_;
    my $tree = HTML::Tree->new();
    $tree->utf8_mode(1); ## wrong call here, no such method, but no warnings on it!
    $tree->parse( $v{content} );

    if ($tree) {
        return $tree;
    } else {
        die "no tree here";
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

生死何惧 2024-12-09 02:17:36

您的代码过于复杂,您使用 utf8::all 并手动解码并立即调用该奇怪的方法。反问,你希望通过这种方式实现什么?我没有耐心找出问题所在和位置的详细信息,特别是因为您没有发布任何使您的程序无法达到预期效果的输入,所以我将其大大简化为一个更简单的输入。这有效:

#!/usr/bin/env perl
use 5.010;
use strict;
use warnings FATAL => ':all';
use File::Slurp qw(read_file);  # autodies on error
use HTML::Tree qw();

my $file = shift;
die 'no file' unless $file;

my $tree = HTML::Tree->new_from_content(
    read_file($file, binmode => ':encoding(UTF-8)')
);

my $title = $tree->look_down(_tag => 'title');
$title->as_HTML(''); # returns a Perl string

Your code is overcomplicated, and you employ utf8::all and decode manually and call that strange method all at once. Rhetorically asking, what do you expect to achieve that way? I do not have the patience to find out the details what goes wrong and where, especially since you did not post any input with which your program fails to do the expected, so I drastically reduce it to a much simpler one. This works:

#!/usr/bin/env perl
use 5.010;
use strict;
use warnings FATAL => ':all';
use File::Slurp qw(read_file);  # autodies on error
use HTML::Tree qw();

my $file = shift;
die 'no file' unless $file;

my $tree = HTML::Tree->new_from_content(
    read_file($file, binmode => ':encoding(UTF-8)')
);

my $title = $tree->look_down(_tag => 'title');
$title->as_HTML(''); # returns a Perl string
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文