【发布时间】:2014-08-25 21:57:39
【问题描述】:
我已经为此工作了一周,但仍然找不到有效的解决方案。我解析包含以 UTF-8 编码的波兰字母的 html 文件。提取我感兴趣的信息后,我将它们保存到文件或打印到控制台,但所有波兰语字符均未正确显示。
我尝试使用我在 Stack Overflow 和其他论坛上找到的所有内容,但出于某种原因对其他人有用的东西对我不起作用。 我用过:
use open qw(:std :utf8);
use HTML::TreeBuilder qw( );
use Object::Destroyer qw( );
#and many others;
这是我的 perl 代码:
use strict;
use warnings;
use feature 'say';
use HTML::TreeBuilder;
use File::Find;
use Encode;
my $location="C:\\MyLocation";
open (MYFILE, '>>data.txt');
sub find_txt {
my $F = $File::Find::name;
if ($F =~ /index.html$/ ) {
my $tr = HTML::TreeBuilder->new->parse_file('index.html');
for my $div ($tr->look_down(_tag => 'h2', 'class' => 'featured')) {
say $div->as_text;
print (MYFILE $div->as_text);
}
for my $div ($tr->look_down(_tag => 'div', 'class' => 'post-content')) {
for my $t ($div->look_down(_tag => 'p')) {
say $t->as_text;
print (MYFILE $t->as_text);
}
}
for my $div ($tr->look_down(_tag => 'h4', 'class' => 'related-posts')) {
for my $t ($div->look_down(_tag => 'a')) {
say $t->as_text;
print (MYFILE $t->as_text);
}
}
}
}
find(\&find_txt, $location);
close (MYFILE);
这是导致问题的html文件:
<div class="post-content">
<p>(łac. abacus)</p>
<p>1. płyta będąca najwyższą częścią kolumny</p>
<p>2. w starożytności – deska do liczenia, pierwowzór liczydła</p>
我不确定您是否能够在浏览器中显示波兰语字符,但有些字符由 unicode 编码为 104、106、118、141、143、D3、15A、179、17B、105、107 , 119, 142, 144, F3, 15B, 17A, 17C
【问题讨论】:
-
你需要
use open qw(:std :utf8),你说你已经尝试过了。您如何看待程序的输出?如果您在 Windowscmd控制台上显示它,那么它可能设置为错误的代码页。输入chcp 65001设置为UTF-8 -
我在控制台上显示输出并将输出保存到文件中。在这两种情况下,结果都是一样的。
标签: html perl encoding utf-8 special-characters