0. 参考

【整理】关于http(GET或POST)请求中的url地址的编码(encode)和解码(decode)

python3中的urlopen对于中文url是如何处理的?

中文URL的编码问题

1. rfc1738

  1. 2.1. The main parts of URLs
  2.  
  3. A full BNF description of the URL syntax is given in Section 5.
  4.  
  5. In general, URLs are written as follows:
  6.  
  7. <scheme>:<scheme-specific-part>
  8.  
  9. A URL contains the name of the scheme being used (<scheme>) followed
  10. by a colon and then a string (the <scheme-specific-part>) whose
  11. interpretation depends on the scheme.
  12.  
  13. Scheme names consist of a sequence of characters. The lower case
  14. letters "a"--"z", digits, and the characters plus ("+"), period
  15. ("."), and hyphen ("-") are allowed. For resiliency, programs
  16. interpreting URLs should treat upper case letters as equivalent to
  17. lower case in scheme names (e.g., allow "HTTP" as well as "http").
  18.  
  19. 注意字母不区分大小写

2. python2

2.1

  1. >>> import urllib
  2. >>> url = 'http://web page.com'
  3. >>> url_en = urllib.quote(url) #空格编码为“%20”
  4. >>> url_plus = urllib.quote_plus(url) #空格编码为“+”
  5. >>> url_en_twice = urllib.quote(url_en)
  6. >>> url
  7. 'http://web page.com'
  8. >>> url_en
  9. 'http%3A//web%20page.com'
  10. >>> url_plus
  11. 'http%3A%2F%2Fweb+page.com'
  12. >>> url_en_twice
  13. 'http%253A//web%2520page.com' #出现%25说明是二次编码
  14. #相应解码
  15. >>> urllib.unquote(url_en)
  16. 'http://web page.com'
  17. >>> urllib.unquote_plus(url_plus)
  18. 'http://web page.com'

2.2 URL含有中文

  1. >>> import urllib
  2. >>> url_zh = u'http://movie.douban.com/tag/美国'
  3. >>> url_zh_en = urllib.quote(url_zh.encode('utf-8')) #参数为string
  4. >>> url_zh_en
  5. 'http%3A//movie.douban.com/tag/%E7%BE%8E%E5%9B%BD'
  6. >>> print urllib.unquote(url_zh_en).decode('utf-8')
  7. http://movie.douban.com/tag/美国

3. python3

3.1

  1. >>> import urllib
  2. >>> url = 'http://web page.com'
  3. >>> url_en = urllib.parse.quote(url) #注意是urllib.parse.quote
  4. >>> url_plus = urllib.parse.quote_plus(url)
  5. >>> url_en
  6. 'http%3A//web%20page.com'
  7. >>> url_plus
  8. 'http%3A%2F%2Fweb+page.com'
  9. >>> urllib.parse.unquote(url_en)
  10. 'http://web page.com'
  11. >>> urllib.parse.unquote_plus(url_plus)
  12. 'http://web page.com'

3.2 URl含中文

  1. >>> import urllib
  2. >>> url_zh = 'http://movie.douban.com/tag/美国'
  3. >>> url_zh_en = urllib.parse.quote(url_zh)
  4. >>> url_zh_en
  5. 'http%3A//movie.douban.com/tag/%E7%BE%8E%E5%9B%BD'
  6. >>> urllib.parse.unquote(url_zh_en)
  7. 'http://movie.douban.com/tag/美国'

4. 其他

  1. >>> help(urllib.urlencode)
  2. Help on function urlencode in module urllib:
  3.  
  4. urlencode(query, doseq=0)
  5. Encode a sequence of two-element tuples or dictionary into a URL query string.
  6.  
  7. If any values in the query arg are sequences and doseq is true, each
  8. sequence element is converted to a separate parameter.
  9.  
  10. If the query arg is a sequence of two-element tuples, the order of the
  11. parameters in the output will match the order of parameters in the
  12. input.
  13.  
  14. >>>

URL地址编码和解码的更多相关文章

  1. url在线编码和解码

    在工作中,经常遇到encode之后的url.想查看里面的某个参数的时候,很不直观.今天在网上搜了一下对url在线编码和解码的网站.对我来说,使用起来很方便.而且这个网站里面,不仅仅有对url的编码和解 ...

  2. URL的编码和解码

    URL的编码和解码 参考:阮一峰--关于URL编码 1 为什么要URL编码 在因特网上传送URL,只能采用ASCII字符集 也就是说URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和 ...

  3. .NET Core中如何对Url进行编码和解码

    我们在.NET Core项目中,可以用WebUtility类对Url进行编码和解码,首先我们要确保项目中引入了nuget包:System.Runtime.Extensions 当然这个nuget包默认 ...

  4. java中URL 的编码和解码函数

    java中URL 的编码和解码函数java.net.URLEncoder.encode(String s)和java.net.URLDecoder.decode(String s);在javascri ...

  5. javascript对url进行编码和解码

    这里总结下JavaScript对URL进行编码和解码的三个方法. 为什么要对URL进行编码和解码 只有[0-9[a-Z] $ - _ . + ! * ' ( ) ,]以及某些保留字,才能不经过编码直接 ...

  6. 在线url网址编码、解码

    >>在线url网址编码.解码<<

  7. i春秋url地址编码问题

    i春秋学院是国内比较知名的安全培训平台,前段时间看了下网站,顺便手工简单测试常见的XSS,发现网站搜索功能比较有意思. 其实是对用户输入的内容HTML编码和URL编码的处理方式在这里不合理,提交到乌云 ...

  8. JavaScript对浏览器的URL进行编码、解码

    关于url编码,js有三个函数.有三个解码方法,escape,encodeURI,encodeURIComponent().有三个解码方法,unescapse,decodeURI,decodeURIC ...

  9. JS对url进行编码和解码(三种方式区别)

    Javascript语言用于编码的函数,一共有三个,最古老的一个就是escape().虽然这个函数现在已经不提倡使用了,但是由于历史原因,很多地方还在使用它,所以有必要先从它讲起. escape 和 ...

随机推荐

  1. HTTP请求头信息

    常用请求头 User-Agent : 浏览器信息Host : 服务区域名Referer : 通过哪里的链接过来的Origin : 跨域相关Content-Type : POST和PUT请求的数据类型C ...

  2. struts2框架之文件上传(参考第三天学习笔记)

    上传 1. 上传对表单的要求 * method=post * enctype=multipart/form-data 2. 上传对servlet要求 * getParameter()不能再使用! -- ...

  3. spring集成cxf实现webservice接口功能

    由于cxf的web项目已经集成了Spring,所以cxf的服务类都是在spring的配置文件中完成的.以下是步骤:第一步:建立一个web项目.第二步:准备所有jar包.将cxf_home\lib项目下 ...

  4. codeforces 38G - Queue splay伸展树

    题目 https://codeforces.com/problemset/problem/38/G 题意: 一些人按顺序进入队列,每个人有两个属性,地位$A$和能力$C$ 每个人进入时都在队尾,并最多 ...

  5. 判断鼠标进入容器的方向小Demo

    参考资料: 贤心博客:http://sentsin.com/web/112.html, Math.atan2(y,x) 解释 :http://www.w3school.com.cn/jsref/jsr ...

  6. 026_nginx引用lua遇到的坑

    server { listen 80; listen 443 ssl; server_name www.jyall.com; access_log /data/log/nginx/*.www.jyal ...

  7. 清理messages提示-bash: /var/log/messages: Operation not permitted的处理

    报警提示系统盘容量不足了/var/log下查看messages日志已经很大了,所以就想着把messages清空一下,以此来释放空间.在删除的时候提示没有权限. 看了下日志,发现是大量的haproxy日 ...

  8. /var/run/yum.pid 已被锁定,PID 为 2925 的另一个程序正在运行

    解决办法:直接在终端运行 rm -f /var/run/yum.pid 将该文件删除,然后再次运行yum.

  9. spark-streaming集成Kafka处理实时数据

    在这篇文章里,我们模拟了一个场景,实时分析订单数据,统计实时收益. 场景模拟 我试图覆盖工程上最为常用的一个场景: 1)首先,向Kafka里实时的写入订单数据,JSON格式,包含订单ID-订单类型-订 ...

  10. 协程,twisted

    最原始的请求url_list=[ 'https://www.cnblogs.com/yunxintryyoubest/category/1338759.html', 'https://www.cnbl ...