URL Quoting
【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的更多相关文章
- (转)Python3 模块3之 Urllib之 urllib.parse、urllib.robotparser
原文:https://blog.csdn.net/qq_36148847/article/details/79153738 https://blog.csdn.net/zly412934578/art ...
- python学习笔记——urllib库中的parse
1 urllib.parse urllib 库中包含有如下内容 Package contents error parse request response robotparser 其中urllib.p ...
- clean-css
What is clean-css? Clean-css is a fast and efficient Node.js library for minifying CSS files. Accord ...
- 笔记-urllib-parse
笔记-urllib-parse 1. 简介模块官方解释This module defines a standard interface to break Uniform Resource Locato ...
- 小白学 Python 爬虫(14):urllib 基础使用(四)
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 小白学 Python 爬虫(3):前置准备(二)Li ...
- HTML URL地址解析
通过JavaScript的location对象,可获取URL中的协议.主机名.端口.锚点.查询参数等信息. 示例 URL:http://www.akmsg.com/WebDemo/URLParsing ...
- URL安全的Base64编码
Base64编码可用于在HTTP环境下传递较长的标识信息.在其他应用程序中,也常常需要把二进制数据编码为适合放在URL(包括隐藏表单域)中的形式.此时,采用Base64编码不仅比较简短,同时也具有不可 ...
- Android业务组件化之URL Scheme使用
前言: 最近公司业务发展迅速,单一的项目工程不再适合公司发展需要,所以开始推进公司APP业务组件化,很荣幸自己能够牵头做这件事,经过研究实现组件化的通信方案通过URL Scheme,所以想着现在还是在 ...
- ASP.NET Core的路由[1]:注册URL模式与HttpHandler的映射关系
ASP.NET Core的路由是通过一个类型为RouterMiddleware的中间件来实现的.如果我们将最终处理HTTP请求的组件称为HttpHandler,那么RouterMiddleware中间 ...
随机推荐
- ADC 电源监测
我能为别人做点什么?这是我最近在思考的问题. 看了 ADC 电源监测代码,觉得对 ADC 的理解不到位,代码中有很多部分都不懂.如: 1. 为什么初始化的时候管脚设置为输出? 2. ADC 采集到的值 ...
- Mac OS X 上Lua的安装方法
先在Mac OS的终端查询下本机是否已安装Lua Last login: Thu Jul 10 07:54:48 on ttys000 keshans-Mac-mini:~ keshan$ lua - ...
- Oracle 学习之路开始
今年刚毕业,从毕业到现在工作正式工作四个多月了(实习的几个月就不算了).工作之中遇到的困难不少,学到的东西也不少:但是感觉学到的东西还是不够,毕竟java水很深啊. 现在工作中并不是每天都能学到新的东 ...
- ajax请求后弹开新页面被浏览器拦截
window.open()我想应该很多人都不陌生吧,它可以实现除用a标签以外来实现打开新窗口! 最近开发项目用到时,却遇到了麻烦,本来好好的弹出窗口,结果被浏览器无情的给拦截了! 代码如下: $.ge ...
- Squid
事件:由于我们在运维过程中需要升级或安装新的开源软件或组件时,相关的依赖包或基础包非常非常多. 因安全限制,对于没有访问internet权限的服务器,在执行安装或升级过程中就非常容易出错. 所以我们需 ...
- 是不是content-type: text/html的数据包一到,浏览器就肯定刷新页面?
整理自:http://q.cnblogs.com/q/54726/ 是不是content-type: text/html的数据包一到,浏览器就肯定刷新页面? 或者说,浏览器收到的状态正常的conten ...
- node版本管理器nvm(服务器项目相关)
git项目 https://github.com/creationix/nvm 1.下载并安装NVM脚本 curl https://raw.githubusercontent.com/creation ...
- [转]N种内核注入DLL的思路及实现
内核注入,技术古老但很实用.现在部分RK趋向无进程,玩的是SYS+DLL,有的无文件,全部存在于内存中.可能有部分人会说:“都进内核了.什么不能干?”.是啊,要是内核中可以做包括R3上所有能做的事,软 ...
- activiti自定义流程之整合(七):完成我的申请任务
在上一篇的获得我的申请中,可以看到js代码中还包含了预览和完成任务的代码,既然上一篇已经罗列了相关代码,这里也就不重复. 那么需要补充的是,在上边的完成任务的js代码中,我们还调用了getTaskFo ...
- android.view.WindowLeaked
08-30 13:17:05.645 25543-25543/com.tongyan.nanjing.subway E/WindowManager: android.view.WindowLeaked ...