0. 参考

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

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

中文URL的编码问题

1. rfc1738

2.1. The main parts of URLsA 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>) followedby a colon and then a string (the <scheme-specific-part>) whoseinterpretation depends on the scheme.Scheme names consist of a sequence of characters. The lower caseletters "a"--"z", digits, and the characters plus ("+"), period("."), and hyphen ("-") are allowed. For resiliency, programsinterpreting URLs should treat upper case letters as equivalent tolower case in scheme names (e.g., allow "HTTP" as well as "http").

注意字母不区分大小写

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 >>>

转载于:https://www.cnblogs.com/my8100/p/7002876.html

URL地址编码和解码相关推荐

  1. 爬虫【3】URL地址编码

    爬虫[3]URL地址编码 爬虫回顾: 爬虫[1]打开网站,获取信息 爬虫[2]重构UserAgent 为什么要进行编码? 在百度中搜索:b站你可以看到上方的url是这样的 可以看到bai.com/s? ...

  2. url在线编码和解码

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

  3. URL 的编码和解码

    URL 的编码和解码 1 什么是 URL? URL(Uniform Resource Locator):统一资源定位符,它是用来表示互联网上的某个资源地址,互联网上的每个文件都有一个唯一的 URL,它 ...

  4. go url 参数编码和解码

    为什么80%的码农都做不了架构师?>>>    1 在做支付的时候会涉及到 url参数编码和解码,然后转换成自己想要的格式 app_id=2016073100129537&b ...

  5. python url解码_对python中url参数编码与解码的实例详解

    一.简介 在python中url,对于中文等非ascii码字符,需要进行参数的编码与解码. 二.关键代码 1.url编码 对字符串编码用urllib.parse包下的quote(string, saf ...

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

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

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

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

  8. linux url解码,js对url进行编码和解码(三种方式区别)

    *** 只有 0-9[a-Z] $ - _ . + ! * ' ( ) , 以及某些保留字,才能不经过编码直接用于 URL. ***例如:搜索的中文关键字,复制网址之后再粘贴就会发现该URL已经被转码 ...

  9. js uri解码_js对url进行编码和解码(三种方式区别)(转)

    *** 只有 0-9[a-Z] $ - _ . + ! * ' ( ) , 以及某些保留字,才能不经过编码直接用于 URL. ***例如:搜索的中文关键字,复制网址之后再粘贴就会发现该URL已经被转码 ...

  10. js 对url进行编码和解码

    三种编码和解码函数: encodeURI和 decodeURI 它着眼于对整个URL进行编码,因此除了常见的符号以外,对其他一些在网址中有特殊含义的符号"; / ? : @ & = ...

最新文章

  1. Ogre 2011-11-29
  2. 九度OJ 区间问题 10000个随机正负数生成
  3. DL:深度学习框架Pytorch、 Tensorflow各种角度对比
  4. elk集群配置配置文件中节点数配多少
  5. go interface{}类型转换
  6. 30道经典SQL面试题讲解(1-10)
  7. OpenCV案例(二):选取圆对象
  8. DBeaver 连接 Oracle
  9. 山东大学软件学院计算机组成原理课程设计实验三
  10. 关于ASCII码的转换
  11. 华为交换机初始化_华为交换机初始设置
  12. java get中文乱码怎么解决_java中get请求中文乱码怎么办?
  13. vector的哈希值 Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined) C
  14. 论文复现-金融风控模型案例
  15. java 社交网站_java学习之电影《社交网络》Facemash算法实现
  16. 科大讯飞,百度,思必驰,云知声四款识别引擎降噪算法性能对比
  17. win10自动连接WIFI的批处理文件
  18. 0.前言 与 Eigen库的使用整理
  19. web -【在线聊天】
  20. window自带的常见工具

热门文章

  1. 如何求matlab的in(2.0375),东南大学Matlab作业1.doc
  2. cdn对动态网站有作用吗_cdn是什么和作用有些
  3. linux数据库定期备份,linux数据库定期备份
  4. socket的java实现_Socket之Java实现普通版本
  5. Eclipse的工作空间与项目
  6. Docker系列(六)镜像与仓库
  7. 如何快速将一个lista集合中的部分字段值组合成新的的listb部分*
  8. SQL 模糊表名查询
  9. Shell脚本中替换字符串等操作
  10. [转]win7添加xp的快速启动栏