2016-10-20 4 views
3

일본어 문자가 포함 된 HTML 문서를 구문 분석하려면 HTML::TokeParser 을 사용하는 데 어려움을 겪고 있습니다.일본어 문자 비교 문제

여기 내 코드입니다 :

use utf8; 

use Encode qw(decode encode is_utf8); 
use Encode::Guess; 
use Data::Dumper; 
use LWP::UserAgent; 
use HTTP::Cookies; 
use Cwd; 
use HTML::TokeParser; 

my $local_dir = getcwd; 

my $browser = LWP::UserAgent->new(); 

my $cookie_jar = HTTP::Cookies->new(
    file  => $local_dir . "/cookies.lwp", 
    autosave => 1, 
); 

$browser->cookie_jar($cookie_jar); 

push @{ $browser->requests_redirectable }, 'POST'; 
$browser->requests_redirectable; 

my $response = $browser->get("http://www.yahoo.co.jp/"); 
my $html = $response->content; 
print $html; 
utf8::decode($html); 

my $p = HTML::TokeParser->new(\$html); 

# dispatch table with subs to handle the different types of tokens 

my %dispatch = (
    S => sub { $_[0]->[4] }, # Start tag 
    E => sub { $_[0]->[2] }, # End tag 
    T => sub { $_[0]->[1] }, # Text 
    C => sub { $_[0]->[1] }, # Comment 
    D => sub { $_[0]->[1] }, # Declaration 
    PI => sub { $_[0]->[2] }, # Process Instruction 
); 

while (my $token = $p->get_tag('a')) { 
     print $p->get_trimmed_text if $p->get_trimmed_text eq '社会的責任'; 
     print "\n"; 
} 

이 내 터미널에 아무것도 표시되지 않습니다,하지만 난 그냥 다음 print $p->get_trimmed_text을 할 경우 출력은 OK입니다. 그것은 비교가 작동하지 않는 것 같습니다처럼

0000000 490a 746e 7265 656e 2074 7845 6c70 726f 
0000010 7265 81e3 e4ae 92ba 8fe6 e89b a8a1 a4e7 
0000020 e3ba ab81 81e3 e3a4 8481 81e3 0aa6 9fe7 
0000030 e5b3 9db7 81e9 e3bc 8982 9be5 e5bd 8586 
0000040 a4e5 e396 ae81 83e3 e397 ad83 82e3 e3b4 
0000050 ab83 83e3 e395 a182 83e3 e3bc 8c81 86e7 
0000060 e68a ac9c 94e6 e6af b48f 320a e334 ab82 
0000070 89e6 e380 ae81 b4e7 e885 8991 90e5 e68d 
0000080 8089 82e3 e692 a597 b8e5 e3b0 8a82 82e3 
0000090 e3b3 bc83 82e3 e4b9 95bb abe7 e38b a681 
00000a0 81e3 e7a7 b9b4 bbe4 0a8b 83e3 e39e af82 
00000b0 83e3 e389 8a83 83e3 e3ab 8983 82e3 e384 
00000c0 8783 83e3 e38b bc83 82e3 e3ba ae81 81e3 
00000d0 e58a 97be 81e3 e3aa af82 83e3 e3bc 9d83 
00000e0 83e3 e9b3 8d85 bfe4 0aa1 a8e8 e88e 96ab 
00000f0 bce4 e39a 8c80 83e3 e392 a983 83e3 e3aa 
0000100 bc83 b0e6 e58f 9d8b 88e5 e3a9 8d80 3235 
0000110 e525 9986 9ce7 4e9f 5745 e50a a7a4 98e9 

: 여기

print $p->get_trimmed_text에 해당하는 몇 가지 16 진 덤프 라인입니다.

서버에만 설치된 유일한 모듈이며 다른 것을 설치할 수 없기 때문에 HTML::TokeParser 만 사용할 수 있습니다.

+0

HTML 페이지가 코드의 문자열과 다른 유니 코드 정규화를 사용합니까? http://www.modernperlbooks.com/mt/2013/01/why-unicode-normalization-matters.html – tripleee

+0

어떻게 확인할 수 있습니까?내가 공유 한 링크에서 이것을 사용하면 유니 코드 :: 정규화를 사용하십시오. 열린 qw/: std : utf8 /을 사용하십시오. 비교하지 않고 print $ p-> get_trimmed_text를 출력해도 가비지가됩니다. – user2360915

+0

흠, 어떤 쓰레기? 어떤 Perl 버전입니까? 어쨌든, 그냥 생각 - 나는 틀린 것을 여기서 알지 못한다. – tripleee

답변

1

ikegami's answer을 참조하십시오. Mine은 코드의 실제 문제를 해결하지 못하는 대체 방법입니다.

구조에

Unicode::Collate


!

아래 코드를 추가했습니다.

use Unicode::Collate; 
use open qw/:std :utf8/; 
my $Collator = Unicode::Collate->new(); 
sub compare_strs 
{ 
    my ($str1, $str2) = @_; 
    # Treat vars as strings by quoting. 
    # Possibly incorrect/irrelevant approach. 
    return $Collator->cmp("$str1", "$str2"); 
} 

참고compare_strs 서브 루틴을 리턴 1 또는 0 ($ STR1는 $의 STR2보다 큰 경우) 또는 1 ($ STR1에 $ STR2 미만인 경우 ($ STR1는 $의 STR2 동일한 경우)).

use strict; 
use warnings; 
use utf8; 
use Unicode::Collate; 
use open qw/:std :utf8/; 
use Encode qw(decode encode is_utf8); 
use Encode::Guess; 
use Data::Dumper; 
use LWP::UserAgent; 
use HTTP::Cookies; 
use Cwd; 
use HTML::TokeParser; 
my $local_dir = getcwd; 
my $browser = LWP::UserAgent->new(); 
my $cookie_jar = HTTP::Cookies->new(
    file  => $local_dir . "/cookies.lwp", 
    autosave => 1, 
); 
$browser->cookie_jar($cookie_jar); 
push @{ $browser->requests_redirectable }, 'POST'; 
$browser->requests_redirectable; 
my $Collator = Unicode::Collate->new(); 
sub compare_strs 
{ 
    my ($str1, $str2) = @_; 
    # Treat vars as strings by quoting. 
    # Possibly incorrect/irrelevant approach. 
    return $Collator->cmp("$str1", "$str2"); 
} 
my $response = $browser->get("http://www.yahoo.co.jp/"); 
my $html = $response->content; 
#print $html; 
utf8::decode($html); 
my $p = HTML::TokeParser->new(\$html); 

# dispatch table with subs to handle the different types of tokens 
my %dispatch = (
    S => sub { $_[0]->[4] }, # Start tag 
    E => sub { $_[0]->[2] }, # End tag 
    T => sub { $_[0]->[1] }, # Text 
    C => sub { $_[0]->[1] }, # Comment 
    D => sub { $_[0]->[1] }, # Declaration 
    PI => sub { $_[0]->[2] }, # Process Instruction 
); 

my $string = '社会的責任'; 
while (my $token = $p->get_tag('a')) { 
     my $text = $p->get_trimmed_text; 
     unless (compare_strs($text, $string)){ 
      print $text; 
      print "\n"; 
     } 
} 

가 출력

을 :

[email protected]:~/Desktop$ perl test.pl 
社会的責任 
+2

OMG! 대단히 감사합니다 ! 굉장해. 날 구해줘. – user2360915

+0

하하. 천만에요. 이러한 작업은 일반적으로 복잡하고 지루한 작업이지만, [Perl] (https://www.perl.org/) 및 [CPAN] (http://www.cpan.org/modules/) 모듈 덕분에 쉬운 삶 : –

+0

흠, 난 그냥 로컬 서버와 야후에 대한 동일한 일을 시도, compare_strs는 -1 모든 시간을 반환 : ( – user2360915

6

당신은 같은 문자열을 반환 $p->get_trimmed_text에 두 통화를 기대하지만, 다른 토큰 매번 반환이 호출 아래

은 전체 작업 코드 . 당신은 HTML이 UTF-8을 사용하여 인코딩한다고 가정해서는 안

my $text = $p->get_trimmed_text; 
print $text if $text eq '社会的責任'; 

print $p->get_trimmed_text if $p->get_trimmed_text eq '社会的責任'; 

교체합니다.

my $html = $response->decoded_content; 

my $html = $response->content; 
utf8::decode($html); 

교체는 또한 당신의 출력을 인코딩 할 필요가있다. 한 가지 방법은 다음을 추가하는 것입니다.

use encode ':std', ':encoding(UTF-8)'; 
+1

그게 간단했다. +1. –

+1

예. 에스. 인코딩 문제가 서버에서 반환 된 것으로 나타났습니다. 이것이 내가 두 번째 문제점을 수정 한 방법입니다. 어쨌든 너 둘 다 고마워. – user2360915