urlencode与unquote

当url地址含有中文或者“/”的时候,这是就需要用做urlencode一下编码转换。

在python3中,urllib和urllib2进行了合并,现在只有一个urllib模块,urllib和urllib2的中的内容整合进了urllib.request,urlparse整合进了urllib.parse

中文转化为%E等(中文编码)

  1. urlencode 参数为字典 ``` import urllib.parse data={“name”:”王尼玛”,”age”:”/“,”addr”:”abcdef”} print(urllib.parse.urlencode(data))

输出:addr=abcdef&name=%E7%8E%8B%E5%B0%BC%E7%8E%9B&age=%2F

  1. 2.
  2. quote 参数为字符串

import urllib.parse print(urllib.parse.quote(“hahaha你好啊!”))

输出为:hahaha%E4%BD%A0%E5%A5%BD%E5%95%8A%EF%BC%81

  1. <a name="e09fbe77"></a>
  2. #### %E等解析为中文(中文解码)
  3. 只有unquote()函数

import urllib.parse

print(urllib.parse.unquote(“hahaha%E4%BD%A0%E5%A5%BD%E5%95%8A%EF%BC%81”))

输出为:hahaha你好啊! ```

参考链接:python3中urllib的基本使用