我需要执行谷歌搜索来检索查询结果的数量。我在这里找到了答案 -来自 Python 应用程序的 Google 搜索
但是,对于少数查询,我收到以下错误。我认为查询有 unicode 字符。
UnicodeDecodeError:“ascii”编解码器无法解码位置 28 中的字节 0xc3:序数不在范围内(128)
我搜索了谷歌,发现我需要将 unicode 转换为 ascii,并找到下面的代码。
def convertToAscii(text, action):
temp = unicode(text, "utf-8")
fixed = unicodedata.normalize('NFKD', temp).encode('ASCII', action)
return fixed
except Exception, errorInfo:
print errorInfo
print "Unable to convert the Unicode characters to xml character entities"
raise errorInfo
如果我使用操作忽略,它会删除这些字符,但如果我使用其他操作,我会遇到异常。
任何想法,如何处理这个?
谢谢
==编辑==我正在使用下面的代码进行编码,然后执行搜索,这会引发错误。
查询 = urllib.urlencode({'q': searchfor})