【问题标题】:Polish characters in Perl [HTML::TreeBuilder and utf8 input files]Perl 中的波兰字符 [HTML::TreeBuilder 和 utf8 输入文件]
【发布时间】: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 &#8211; 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),你说你已经尝试过了。您如何看待程序的输出?如果您在 Windows cmd 控制台上显示它,那么它可能设置为错误的代码页。输入chcp 65001 设置为UTF-8
  • 我在控制台上显示输出并将输出保存到文件中。在这两种情况下,结果都是一样的。

标签: html perl encoding utf-8 special-characters


【解决方案1】:

HTML::TreeBuilder parse_file - 字符集自动检测

您可以使用给定的字符集显式打开文件

...
open (my $MYFILE, '>>:utf8','index.html'); # explicitly open MYFILE with utf8 charset
...
my $tr = HTML::TreeBuilder->new->parse_file($MYFILE);
...

使用 IO::HTML 对打开的文件进行自动字符集检测。

...
use IO::HTML;                 # exports html_file by default
...
my $tr = HTML::TreeBuilder->new->parse_file(html_file('index.html'));
....

man HTML::TreeBuilder

parse_file
   ....
   When you pass a filename to "parse_file", HTML::Parser opens it in binary mode,
   which means it's interpreted as Latin-1 (ISO-8859-1).  If the file
   is in another encoding, like UTF-8 or UTF-16, this will not do the right thing.
....
SEE ALSO
   ....
   For opening a HTML file with automatic charset detection: IO::HTML.

【讨论】:

  • HTML::TreeBuilder 的下一个主要版本将自动使用 IO::HTML。您可以通过测试trial release 来提供帮助。
  • 使用 IO::HTML;没有任何区别。我会尝试试用版。
  • use utf8;binmode(STDOUT,':utf8');open (MYFILE, '&gt;&gt;:utf8','data.txt'); 检查它——我的comp 上的混合固定问题。否则报告 `say('ąćę') 打印/显示正确的字符。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-09-13
  • 1970-01-01
  • 2017-11-29
  • 1970-01-01
  • 1970-01-01
  • 2012-02-05
  • 2013-02-25
相关资源
最近更新 更多