我试图从一个使用beauthoulsoup和urllib2的网站获取一些西班牙语文本。我现在得到这个:¡Hola! ¿Cómo estás?
。
我尝试过在相关线程上应用不同的unicode函数,但似乎没有什么能解决我的问题:
# 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)
thetext
是类型<class 'BeautifulSoup.NavigableString'>
。打印它将返回一个Unicode字符串,该字符串将在输出终端编码中进行编码:输出(在Windows控制台中):
^{pr2}$这将适用于为支持正在打印的Unicode字符而配置的任何终端。在
如果您的终端配置了不支持您尝试打印的Unicode字符的编码,您将得到
UnicodeEncodeError
。在在该类型上使用
str
将返回一个字节字符串…在本例中是用UTF-8编码的。如果你在一个配置了UTF-8的终端上打印它,你会得到一个错误的显示。在相关问题 更多 >
编程相关推荐