gpt4 book ai didi

python - 在 Python 中使用 urllib 打开 UTF16 URL

转载 作者:太空宇宙 更新时间:2023-11-04 03:59:17 27 4
gpt4 key购买 nike

<分区>

我正在尝试使用 Google Translate API 将卡纳达语(因此编码为 utf-16)的文本翻译成英语。手动输入我的 URL,插入我的 google api key 后,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)

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com