URL地址编码和解码
0. 参考
【整理】关于http(GET或POST)请求中的url地址的编码(encode)和解码(decode)
python3中的urlopen对于中文url是如何处理的?
中文URL的编码问题
1. rfc1738
2.1. The main parts of URLs
A full BNF description of the URL syntax is given in Section 5.
In general, URLs are written as follows:
<scheme>:<scheme-specific-part>
A URL contains the name of the scheme being used (<scheme>) followed
by a colon and then a string (the <scheme-specific-part>) whose
interpretation depends on the scheme.
Scheme names consist of a sequence of characters. The lower case
letters "a"--"z", digits, and the characters plus ("+"), period
("."), and hyphen ("-") are allowed. For resiliency, programs
interpreting URLs should treat upper case letters as equivalent to
lower case in scheme names (e.g., allow "HTTP" as well as "http").
注意字母不区分大小写
2. python2
2.1
>>> import urllib
>>> url = 'http://web page.com'
>>> url_en = urllib.quote(url) #空格编码为“%20”
>>> url_plus = urllib.quote_plus(url) #空格编码为“+”
>>> url_en_twice = urllib.quote(url_en)
>>> url
'http://web page.com'
>>> url_en
'http%3A//web%20page.com'
>>> url_plus
'http%3A%2F%2Fweb+page.com'
>>> url_en_twice
'http%253A//web%2520page.com' #出现%25说明是二次编码
#相应解码
>>> urllib.unquote(url_en)
'http://web page.com'
>>> urllib.unquote_plus(url_plus)
'http://web page.com'
2.2 URL含有中文
>>> import urllib
>>> url_zh = u'http://movie.douban.com/tag/美国'
>>> url_zh_en = urllib.quote(url_zh.encode('utf-8')) #参数为string
>>> url_zh_en
'http%3A//movie.douban.com/tag/%E7%BE%8E%E5%9B%BD'
>>> print urllib.unquote(url_zh_en).decode('utf-8')
http://movie.douban.com/tag/美国
3. python3
3.1
>>> import urllib
>>> url = 'http://web page.com'
>>> url_en = urllib.parse.quote(url) #注意是urllib.parse.quote
>>> url_plus = urllib.parse.quote_plus(url)
>>> url_en
'http%3A//web%20page.com'
>>> url_plus
'http%3A%2F%2Fweb+page.com'
>>> urllib.parse.unquote(url_en)
'http://web page.com'
>>> urllib.parse.unquote_plus(url_plus)
'http://web page.com'
3.2 URl含中文
>>> import urllib
>>> url_zh = 'http://movie.douban.com/tag/美国'
>>> url_zh_en = urllib.parse.quote(url_zh)
>>> url_zh_en
'http%3A//movie.douban.com/tag/%E7%BE%8E%E5%9B%BD'
>>> urllib.parse.unquote(url_zh_en)
'http://movie.douban.com/tag/美国'
4. 其他
>>> help(urllib.urlencode)
Help on function urlencode in module urllib: urlencode(query, doseq=0)
Encode a sequence of two-element tuples or dictionary into a URL query string. If any values in the query arg are sequences and doseq is true, each
sequence element is converted to a separate parameter. If the query arg is a sequence of two-element tuples, the order of the
parameters in the output will match the order of parameters in the
input. >>>
URL地址编码和解码的更多相关文章
- url在线编码和解码
在工作中,经常遇到encode之后的url.想查看里面的某个参数的时候,很不直观.今天在网上搜了一下对url在线编码和解码的网站.对我来说,使用起来很方便.而且这个网站里面,不仅仅有对url的编码和解 ...
- URL的编码和解码
URL的编码和解码 参考:阮一峰--关于URL编码 1 为什么要URL编码 在因特网上传送URL,只能采用ASCII字符集 也就是说URL只能使用英文字母.阿拉伯数字和某些标点符号,不能使用其他文字和 ...
- .NET Core中如何对Url进行编码和解码
我们在.NET Core项目中,可以用WebUtility类对Url进行编码和解码,首先我们要确保项目中引入了nuget包:System.Runtime.Extensions 当然这个nuget包默认 ...
- java中URL 的编码和解码函数
java中URL 的编码和解码函数java.net.URLEncoder.encode(String s)和java.net.URLDecoder.decode(String s);在javascri ...
- javascript对url进行编码和解码
这里总结下JavaScript对URL进行编码和解码的三个方法. 为什么要对URL进行编码和解码 只有[0-9[a-Z] $ - _ . + ! * ' ( ) ,]以及某些保留字,才能不经过编码直接 ...
- 在线url网址编码、解码
>>在线url网址编码.解码<<
- i春秋url地址编码问题
i春秋学院是国内比较知名的安全培训平台,前段时间看了下网站,顺便手工简单测试常见的XSS,发现网站搜索功能比较有意思. 其实是对用户输入的内容HTML编码和URL编码的处理方式在这里不合理,提交到乌云 ...
- JavaScript对浏览器的URL进行编码、解码
关于url编码,js有三个函数.有三个解码方法,escape,encodeURI,encodeURIComponent().有三个解码方法,unescapse,decodeURI,decodeURIC ...
- JS对url进行编码和解码(三种方式区别)
Javascript语言用于编码的函数,一共有三个,最古老的一个就是escape().虽然这个函数现在已经不提倡使用了,但是由于历史原因,很多地方还在使用它,所以有必要先从它讲起. escape 和 ...
随机推荐
- hibernate框架学习第三天:对象状态、一级缓存、快照等
对象的状态 瞬时状态: 瞬时对象(TO) 应用程序创建出来的对象,不受H3控制 注意:TO对象不具有OID,一旦为TO赋值OID,那么此时就不是TO 持久化状态:持久化对象(PO) 受H3控制的对象, ...
- JS实现多语言方式
应用场景: 在不同移动平台(IOS,Android)上,建立一套与HTML页面通讯框架:主要业务逻辑使用HTML开发:想支持多语言开发. 动机: 通过主动发消息的方式,在页面完成初始化前,获取当前语言 ...
- ebs 12.1.1 单节点多用户安装
本次测试环境:操作系统 oracle linux 6.9 oracle ebs 12.1.1 192.168.20.210 erpapp1.hthorizon.com erpapp1 yum ...
- Expm 10_2 实现Ford-Fulkerson算法,求出给定图中从源点s到汇点t的最大流,并输出最小割。
package org.xiu68.exp.exp10; import java.util.ArrayDeque; import java.util.ArrayList; import java.ut ...
- 解决:org.xml.sax.SAXParseException: 元素类型 "head" 必须由匹配的结束标记 "</head>问题
事件背景: 今天就碰到了这样的问题, org.xml.sax.SAXParseException: 元素类型 "head" 必须由匹配的结束标记 "</head&g ...
- centos6.5磁盘扩容
3台虚拟机都是20G磁盘,用着用着发现不够了,先扩容了一台,各种百度...各种坑,每个人的情况不一样,发现不一样的地方最后立即百度查看.一台扩容成功后,打算再扩容一台,目的是留一个记录.(我是用xsh ...
- grep匹配某个次出现的次数
cat file | grep -c 'xxx' 统计xxx在file中出现的行数 cat file | grep -o 'xxx' 统计xxx在file中出现的次数
- html<meta>标签
1. 定义说明 <meta>提供与页面有关的元数据,元数据是对数据的描述 <meta>总是位于<head></head>中 <meta>定义 ...
- redis客户端、分布式锁及数据一致性
Redis Java客户端有很多的开源产品比如Redission.Jedis.lettuce等. Jedis是Redis的Java实现的客户端,其API提供了比较全面的Redis命令的支持:Redis ...
- laravel 路由模型绑定
我们在使用路由的时候一个很常见的使用场景就是根据资源 ID 查询资源信息: Route::get('task/{id}', function ($id) { $task = \App\Models\T ...