我正在尝试使用 Google Translate API 将卡纳达语(因此编码为 utf-16)的文本翻译成英语。手动输入我的 URL,在插入我的 google api 密钥后,https://www.googleapis.com/language/translate/v2? key =key#&q =ಚಿಂಚೋಳಿ&source=kn&target=en,我能够得到我想要的翻译。
然而,问题是这个 url 是 utf16 编码的。当我尝试使用 urllib 打开 url 时,我从下面收到错误消息。任何有关如何进行或其他方法的建议将不胜感激。
编辑:我相信这个问题可以通过调用 urllib.parse.quote_plus(text) 来解决,其中 text 是 utf16 文本,并用该函数的返回值替换 utf16 文本。
Traceback (most recent call last):
File "<pyshell#19>", line 1, in <module>
urllib.request.urlopen(url)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 156, in urlopen
return opener.open(url, data, timeout)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 469, in open
response = self._open(req, data)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 487, in _open
'_open', req)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 447, in _call_chain
result = func(*args)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 1283, in https_open
context=self._context, check_hostname=self._check_hostname)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/urllib/request.py", line 1248, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/client.py", line 1061, in request
self._send_request(method, url, body, headers)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/client.py", line 1089, in _send_request
self.putrequest(method, url, **skips)
File "/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/http/client.py", line 953, in putrequest
self._output(request.encode('ascii'))
UnicodeEncodeError: 'ascii' codec can't encode characters in position 73-79: ordinal not in range(128)