python - 奇怪的网站编码。是否有可能使用 python 获取它?

标签 python html url encoding character-encoding

例如,我有这句话

Olá mundo!

我需要它写成

ol%E1%20mundo!

创建我的网址。

我需要这个来连接到一些使用这种编码的特定站点。

使用此类编码的站点示例是 Michaellis翻译。

如何使用 python 使用这些类型的编码创建 url?我尝试使用 urllib 和 urllib2,但直到现在我还没有成功。

这里是 another question我的,与此有关。

感谢您的帮助,谢谢。

最佳答案

这是使用 urllibquotestringencode("utf8") 的一种方法:

In  [1]: url = u'Ol\xe1 mundo!'

In  [2]: url.encode("utf8")
Out [2]: 'Ol\xc3\xa1 mundo!'

In  [3]: print url.encode("utf8")
Olá mundo!

In  [4]: urllib.quote(url.encode("utf8"))
Out [4]: 'Ol%C3%A1%20mundo%21'

In  [5]: print urllib.quote(url.encode("utf8"))
Ol%C3%A1%20mundo%21

In  [6]: urllib.unquote(urllib.quote(url.encode("utf8")))
Out [6]: 'Ol\xc3\xa1 mundo!'

In  [7]: print urllib.unquote(urllib.quote(url.encode("utf8")))
Olá mundo!

关于python - 奇怪的网站编码。是否有可能使用 python 获取它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8205674/

相关文章:

python - 不能用 flask 路由到 "/login"?

asp.net-mvc - C# razor url 参数 View

python - 无法在我扭曲的网络资源中测试 reactor.callInThread

python - python中的多线程是一个神话吗?

javascript - 支持拖放的 Html 文件上传文件

html - 如何使用 960.gs 系统制作 3 或 4 列布局?

python - py2neo 中的 Cypher 查询

python - 不可变类型的 ID

html - 无法点击我的导航链接

javascript - MVC3 - Javascript 中的动态 URL 重定向