URL Quoting

  The URL quoting functions focus on taking program data and making it safe for use as URL components by quoting special characters and appropriately encoding non-ASCII text. They also support reversing these operations to recreate the original data from the contents of a URL component.

  URL quoting函数作用是将数据能够作为url的一部分,包括处理特殊字符,及编码非ASCII文本。quoting函数也支持逆转以还原数据。

urllib.parse.quote(string, safe='/', encoding=None, errors=None)

Replace special characters in string using the %xx escape. Letters, digits, and the characters '_.-' are never quoted.

Note that quote(string, safe, encoding, errors) is equivalent to quote_from_bytes(string.encode(encoding, errors), safe).

Example: quote('/El Ni駉/') yields '/El%20Ni%C3%B1o/'.

  

urllib.parse.quote_plus(string, safe='', encoding=None, errors=None)
Like quote(), but also replace spaces by plus signs, as required for quoting HTML form values when building up a query string to go into a URL. Plus signs in the original string are escaped unless they are included in safe. It also does not have safe default to '/'.
Example: quote_plus('/El Ni駉/') yields '%2FEl+Ni%C3%B1o%2F'.
在quote的基础上,把空格变为'+',连'/'也会被转义。
urllib.parse.unquote(string, encoding='utf-8', errors='replace')
string must be a str.
encoding defaults to 'utf-8'. errors defaults to 'replace', meaning invalid sequences are replaced by a placeholder character.
Example: unquote('/El%20Ni%C3%B1o/') yields '/El Ni駉/'.
urllib.parse.unquote_plus(string, encoding='utf-8', errors='replace')
Like unquote(), but also replace plus signs by spaces, as required for unquoting HTML form values.
string must be a str.
Example: unquote_plus('/El+Ni%C3%B1o/') yields '/El Ni駉/'.
urllib.parse.urlencode(query, doseq=False, safe='', encoding=None, errors=None, quote_via=quote_plus)

Convert a mapping object or a sequence of two-element tuples, which may contain str or bytes objects, to a percent-encoded ASCII text string.

The resulting string is a series of key=value pairs separated by '&' characters, where both key and value are quoted using the quote_via function. By default, quote_plus() is used to quote the values, which means spaces are quoted as a '+' character and ‘/’ characters are encoded as %2F, which follows the standard for GET requests (application/x-www-form-urlencoded). An alternate function that can be passed as quote_via is quote(), which will encode spaces as %20 and not encode ‘/’ characters. For maximum control of what is quoted, use quote and specify a value for safe.

The safe, encoding, and errors parameters are passed down to quote_via (the encoding and errors parameters are only passed when a query element is a str).

To reverse this encoding process, parse_qs() and parse_qsl() are provided in this module to parse query strings into Python data structures.

Example

1、Here is an example session that uses the GET method to retrieve a URL containing parameters:

2、The following example uses the POST method instead. Note that params output from urlencode is encoded to bytes before it is sent to urlopen as data:

   POST  encode后的数据要转换为bytes!!!

  

3、The following example uses an explicitly specified HTTP proxy, overriding environment settings:

  

4、The following example uses an explicitly specified HTTP proxy, overriding environment settings:

  

URL Quoting的更多相关文章

  1. (转)Python3 模块3之 Urllib之 urllib.parse、urllib.robotparser

    原文:https://blog.csdn.net/qq_36148847/article/details/79153738 https://blog.csdn.net/zly412934578/art ...

  2. python学习笔记——urllib库中的parse

    1 urllib.parse urllib 库中包含有如下内容 Package contents error parse request response robotparser 其中urllib.p ...

  3. clean-css

    What is clean-css? Clean-css is a fast and efficient Node.js library for minifying CSS files. Accord ...

  4. 笔记-urllib-parse

    笔记-urllib-parse 1. 简介模块官方解释This module defines a standard interface to break Uniform Resource Locato ...

  5. 小白学 Python 爬虫(14):urllib 基础使用(四)

    人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...

  6. HTML URL地址解析

    通过JavaScript的location对象,可获取URL中的协议.主机名.端口.锚点.查询参数等信息. 示例 URL:http://www.akmsg.com/WebDemo/URLParsing ...

  7. URL安全的Base64编码

    Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式.此时,采用Base64编码不仅比较简短,同时也具有不可 ...

  8. Android业务组件化之URL Scheme使用

    前言: 最近公司业务发展迅速,单一的项目工程不再适合公司发展需要,所以开始推进公司APP业务组件化,很荣幸自己能够牵头做这件事,经过研究实现组件化的通信方案通过URL Scheme,所以想着现在还是在 ...

  9. ASP.NET Core的路由[1]:注册URL模式与HttpHandler的映射关系

    ASP.NET Core的路由是通过一个类型为RouterMiddleware的中间件来实现的.如果我们将最终处理HTTP请求的组件称为HttpHandler,那么RouterMiddleware中间 ...

随机推荐

  1. mysql出现启动不了问题

    查询日志后是‘ Fatal error: Can't open and lock privilege tables: Table 'mysql.host' doesn't exist’此错误(less ...

  2. 关于SCRUM站立会议

    查询过后对SCRUM站立会议有了初步的了解 站立会议:在敏捷流程的冲刺阶段中,每一天都会举行项目状况会议,强迫每个人向同伴报告进度,迫使大家把问题摆在明面上,这个会议被称为“scrum”或“每日站立会 ...

  3. colorPrimaryDark无法改变状态栏颜色

    设置完colorPrimaryDark后,这个颜色是改变状态栏的颜色的, colorPrimary是改变标题栏背景色的 发现状态栏一直是灰色. 然后在布局文件中 AndroidMainifest.xm ...

  4. android国际化操作

    1.简单介绍 我们知道在java中通过.properties文件来配置资源文件,一般用的有中文message_zh_CN.properties和英文message_en_US.properties两个 ...

  5. [solr] - spell check

    solr提供了一个spell check,又叫suggestions,可以用于查询输入的自动完成功能auto-complete. 参考文献: https://cwiki.apache.org/conf ...

  6. SpringMVC学习系列(2) 之 经典的HelloWorld实现

    前一篇简单介绍了Spring MVC的一些知识,下面就要开始学习如何把Spring MVC运用到具体的项目中去. 首先还是从一个简单的Hello World项目说起: 我机器的开发环境为: Ubunt ...

  7. bzoj1513: [POI2006]Tet-Tetris 3D

    Description Task: Tetris 3D "Tetris" 游戏的作者决定做一个新的游戏, 一个三维的版本, 在里面很多立方体落在平面板,一个立方体开始落下直到碰上一 ...

  8. try-catch(C# 参考)

    https://msdn.microsoft.com/zh-cn/library/0yd65esw.aspx Try-catch 语句包含一个后接一个或多个 catch 子句的 try 块,这些子句指 ...

  9. Hadoop学习12-配置集群环境

    由于之前虚拟机都是用的桥接方式,有时候没有网络可用,想学习的时候,就狠不方便. 于是研究了一下,希望搭建一个多台虚机组成一个局域网的集群,即host-only方式 1.安装VM,网络选择“host-o ...

  10. git 强制回退服务器上的commit

    假设你有3个commit如下: commit 3 commit 2 commit 1   其中最后一次提交commit 3是错误的,那么可以执行: git reset --hard HEAD~1 你会 ...