美丽汤:爬取西班牙字符问题

2024-05-13 23:50:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从一个使用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)

Tags: thefromimportwikicontentspageutilsurllib2
1条回答
网友
1楼 · 发布于 2024-05-13 23:50:53

thetext是类型<class 'BeautifulSoup.NavigableString'>。打印它将返回一个Unicode字符串,该字符串将在输出终端编码中进行编码:

print thetext

输出(在Windows控制台中):

^{pr2}$

这将适用于为支持正在打印的Unicode字符而配置的任何终端。在

如果您的终端配置了不支持您尝试打印的Unicode字符的编码,您将得到UnicodeEncodeError。在

在该类型上使用str将返回一个字节字符串…在本例中是用UTF-8编码的。如果你在一个配置了UTF-8的终端上打印它,你会得到一个错误的显示。在

相关问题 更多 >