2016-10-29 3 views
0

BeautifulSoupとurllib2を使ってウェブサイトからスペイン語のテキストを取得しようとしています。私は現在、これを得る:¡Hola! ¿Cómo estás?。 私は、関連するスレッド上で見てきたさまざまなUnicode関数を適用しようとしたが、何も私の問題のために動いていないようにみえ:BeautifulSoup:スペイン語の文字を削る

# import the main window object (mw) from aqt 
from aqt import mw 
# import the "show info" tool from utils.py 
from aqt.utils import showInfo 
# import all of the Qt GUI library 
from aqt.qt import * 

from BeautifulSoup import BeautifulSoup 

import urllib2 



wiki = "http://spanishdict.com/translate/hola" 

page = urllib2.urlopen(wiki) 

soup = BeautifulSoup(page) 

dictionarydiv = soup.find("div", { "class" : "dictionary-neodict-example" }) 

dictionaryspans = dictionarydiv.contents 

firstspan = dictionaryspans[0] 

firstspantext = firstspan.contents 

thetext = firstspantext[0] 

thetextstring = str(thetext) 
+0

'print thetextstring'は、UTF-8端末の場合にのみ、@FranciscoCouzoのようになります。 –

+0

OPはWindows上にある可能性があります。 –

答えて

0

thetextはタイプ<class 'BeautifulSoup.NavigableString'>です。印刷が出力端子エンコーディングでエンコードされたUnicode文字列を返す:(Windowsコンソールで)

print thetext 

出力:

¡Hola! ¿Cómo estás? 

これは支持符号化のために構成された任意の端末上で動作しますUnicode文字が印刷されます。

端末が、印刷しようとするUnicode文字をサポートしていないエンコードで設定されている場合は、UnicodeEncodeErrorと表示されます。

このタイプでstrを使用すると、バイト文字列...が返されます。この場合、UTF-8でエンコードされます。あなたがUTF-8で構成された端末以外のものにそれを印刷すると、間違った表示が得られます。

関連する問題