【发布时间】:2019-04-07 10:37:36
【问题描述】:
我有一个问题,我尝试使用 Google Custom search api for python 在结果中进行搜索,但是当我搜索存储在变量中的内容而不是手动编写它们时,它会显示 UnicodeEncodeError: 'ascii' codec can '不编码位置 104 中的字符'\xa2':序数不在范围内(128)。当我用
解决它时 .encode('ascii', 'ignore').decode('ascii')
它显示另一个错误,例如 google 自定义搜索
TypeError: can only concatenate str (not "bytes") to str.
PD:我也尝试过诸如 str() 或 .decode 之类的东西。
编辑:当然,存储在变量中的输入来自读取图像文本的 Pytesseract。因此,我将此信息存储在一个变量中,然后尝试在谷歌自定义搜索 API 中搜索此信息。当它显示 Unicode 错误时,我查看了 stackoverflow 的解决方案,发现我可以尝试对变量进行 .decode 以不再出现此问题。事实上这个问题已经解决了,但现在又出现了一个问题,那就是 TypeError: can only concatenate str (not "bytes") to str。所以,我不能使用 .decode 函数,因为它会显示另一个错误。我能做什么?
编辑 2.0
text_photo = pytesseract.image_to_string(img2) #this will read the text and put it in a variable
text_photo = text_photo.replace('\r', '').replace('\n', '') #this will elimininate de /n
rawData = urllib.request.urlopen(url_google_1 + text_photo1 + '+' + text_photo2 + url_google_2).read()
url_google 1 包含用于谷歌搜索的链接的第一部分(api 密钥...),第二部分包含我想从谷歌获得的内容。在中间我添加了变量,因为它是我想要搜索的。如果我写你好,问题是 tesseract 写入的格式不兼容我尝试使用 str(text_photo) 和 .decode 但不起作用 json_data = json.loads(rawData)
【问题讨论】:
标签: python unicode python-unicode google-custom-search