如何打开带有非 utf-8 参数的 URL

发布于 2024-08-10 02:19:54 字数 99 浏览 2 评论 0原文

使用 Python,我需要通过查询字符串将非 utf-8 编码数据(特别是 shift-jis)传输到 URL。 我应该如何传输数据?引用一下?编码为utf-8?

谢谢

Using Python I need to transfer non utf-8 encoded data (specifically shift-jis) to a URL via the query string.
How should I transfer the data? Quote it? Encode in utf-8?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

还不是爱你 2024-08-17 02:19:54

查询字符串参数是基于字节的。虽然 IRI 到 URI 和键入的非 ASCII 字符通常使用 UTF-8,但没有什么强制您以该编码发送或接收自己的参数。

因此,对于 Shift-JIS(实际上通常是 cp932,该编码的 Windows 扩展):

foo= u'\u65E5\u672C\u8A9E' # 日本語
url= 'http://www.example.jp/something?foo='+urllib.quote(foo.encode('cp932'))

在 Python 3 中,您可以在 quote 函数本身中执行此操作:

foo= '\u65E5\u672C\u8A9E'
url= 'http://www.example.jp/something?foo='+urllib.parse.quote(foo, encoding= 'cp932')

Query string parameters are byte-based. Whilst IRI-to-URI and typed non-ASCII characters will typically use UTF-8, there is nothing forcing you to send or receive your own parameters in that encoding.

So for Shift-JIS (actually typically cp932, the Windows extension of that encoding):

foo= u'\u65E5\u672C\u8A9E' # 日本語
url= 'http://www.example.jp/something?foo='+urllib.quote(foo.encode('cp932'))

In Python 3 you do it in the quote function itself:

foo= '\u65E5\u672C\u8A9E'
url= 'http://www.example.jp/something?foo='+urllib.parse.quote(foo, encoding= 'cp932')
情未る 2024-08-17 02:19:54

我不知道 unicode 与此有什么关系,因为查询字符串是一串字节。您可以使用 urllib 中的引用函数来引用纯字符串,以便它们可以被传递在查询字符串中。

I don't know what unicode has to do with this, since the query string is a string of bytes. You can use the quoting functions in urllib to quote plain strings so that they can be passed within query strings.

深爱不及久伴 2024-08-17 02:19:54

通过“查询字符串”,您的意思是像 http:/{URL}?data=XYZ? 中的 HTTP GET?

您可以使用 -_ 作为替代字符,通过 base64.b64encode 对您拥有的数据进行编码,以确保 URL 安全。请参阅此处

By the »query string« you mean HTTP GET like in http:/{URL}?data=XYZ?

You have encoding what ever data you have via base64.b64encode using -_ as alternative character to be URL safe as an option. See here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文