Re: [問題] 抓回來的網頁去除html標籤後出現部份亂碼

看板Perl作者 (jet)時間17年前 (2007/12/19 00:59), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/3 (看更多)
※ 引述《redmist (....N )》之銘言: : 寫了一支程式到網路上抓資料,用HTML::FormatText模組去除html標籤後會出現部份 : 亂碼(不是全部,甚至只有一小部份),是什麼原因呢? : code如下 : use LWP::UserAgent; : use HTML::Parse; : use HTML::FormatText; : my $ua = LWP::UserAgent->new; : $ua->agent("MyApp/0.1 "); : my $word = '生氣'; : my $response = $ua->post( : 'http://www.sinica.edu.tw/ftms-bin/scripts/look_for_sym.pl', : [ : 'kw0' => $word : ], : ); : if ($response->is_success) { : my $res = $response->content; : #$res =~s/\r\n/\n/; : $ascii = HTML::FormatText->new->format(parse_html($res)); : print $ascii; : } : else { : die $response->status_line; : } : 印出來的部份結果如下 : (1) 書卷氣 : (2) 書生氣 : 共有2茼P義詞 : 為何會有小部份奇怪的亂碼呢? 好久沒有回問題了 回一下XD 2個同義詞 hex= 32 ad d3 a6 50 b8 71 b5 fc 2茼P義詞 hex= 32 d3 a6 50 b8 71 b5 fc ad在處理中被吃掉了 hex ad = '-' 雖然不知道為什麼會這樣 還有有方法可以解決 1.在最前面加上 use Encode; 2.修改內文 if ($response->is_success) { my $res = decode('big5',$response->content); $ascii = HTML::FormatText->new->format(parse_html($res)); $ascii = encode('big5',$ascii); print $ascii; } 只要是中文問題 用這招通常都有效:P -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.121.197.133 ※ 編輯: jjt 來自: 140.121.197.133 (12/19 01:00)

12/19 12:38, , 1F
可以了 真是感謝吶
12/19 12:38, 1F
文章代碼(AID): #17P_nx8W (Perl)
文章代碼(AID): #17P_nx8W (Perl)