笔记-urllib-parse
笔记-urllib-parse
1. 简介
模块官方解释
This module defines a standard interface to break Uniform Resource Locator (URL) strings up in components (addressing scheme, network location, path etc.), to combine the components back into a URL string, and to convert a “relative URL” to an absolute URL given a “base URL.”
简单来说就是处理/解析URL的。
2. 解析函数
2.1. urllib.parse
urllib.parse.urlparse(urlstring, scheme='', allow_fragments=True)
函数用于将一个URL解析成六个部分,返回一个元组,URL的格式为:scheme://netloc/path;parameters?query#fragment;包含六个部分,元组中每一个元素都是一个字符串,可以为空,这六个部分均不能再被分割成更小的部分;
以下为返回的元组元素:
元素 编号 值 值不存在时默认值
scheme 0 请求 一定存在
netloc 1 网址 空字符串
path 2 分层路径 空字符串
params 3 参数 空字符串
query 4 查询组件 空字符串
fragment 5 标识符 空字符串
username 用户名 None
password 密码 None
hostname 主机名 None
port 端口号 None
典型使用:
import urllib.parse
o = urllib.parse.urlparse('https://blog.csdn.net/qq_41769259/article/details/79434494')
print(o)
2.2. parse_qs
urllib.parse.parse_qs(qs, keep_blank_values=False, strict_parsing=False, encoding='utf-8', errors='replace')
这个函数主要用于分析URL中query组件的参数,返回一个key-value对应的字典格式。
简单应用:
print(urllib.parse.parse_qs("FuncNo=9009001&username=1"))
输出:
{'FuncNo': ['9009001'], 'username': ['1']}
urllib.parse.parse_qsl(qs, keep_blank_values=False, strict_parsing=False, encoding=’utf-8’, errors=’replace’)
这个函数和urllib.parse.parse_qs()作用一样,唯一的区别就是这个函数返回值是list形式;
2.3. urlunparse()
urllib.parse.urlunparse(parts)
Construct a URL from a tuple as returned by urlparse(). The parts argument can be any six-item iterable. This may result in a slightly different, but equivalent URL, if the URL that was parsed originally had unnecessary delimiters (for example, a ? with an empty query; the RFC states that these are equivalent).
这个函数可以将urlparse()分解出来的元组组装成URL;
o = urllib.parse.urlparse('https://www.zhihu.com/question/50056807/answer/2235669')
print(o)
print(urllib.parse.urlunparse(o))
output:
ParseResult(scheme='https', netloc='www.zhihu.com', path='/question/50056807/answer/2235669', params='', query='', fragment='')
https://www.zhihu.com/question/50056807/answer/2235669
2.4. urlsplit()
urllib.parse.urlsplit(urlstring, scheme='', allow_fragments=True)
This is similar to urlparse(), but does not split the params from the URL. This should generally be used instead of urlparse() if the more recent URL syntax allowing parameters to be applied to each segment of the path portion of the URL (see RFC 2396) is wanted. A separate function is needed to separate the path segments and parameters. This function returns a 5-tuple: (addressing scheme, network location, path, query, fragment identifier).
这个函数和urlparse()功能类似,但是不会将param分离出来;相比urlparse()少一个param元素,返回的元组元素参照urlparse()的元组表。
2.5. urljoin()
urllib.parse.urljoin(base, url, allow_fragments=True)
将一个基址和一个相对地址组合成新的URL。
>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html', 'FAQ.html')
'http://www.cwi.nl/%7Eguido/FAQ.html'
值得注意的是如果被组合的地址是绝对地址,那么结果会不一样,在编程中应通过预处理尽量避免这种情况:
>>> urljoin('http://www.cwi.nl/%7Eguido/Python.html',
... '//www.python.org/%7Eguido')
'http://www.python.org/%7Eguido'
3. quote
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 if that task isn’t already covered by the URL parsing functions above.
这个模块的主要作用就是通过引入合适编码和特殊字符对URL进行安全重构,并且可以反向解析
3.1. quote()
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. By default, this function is intended for quoting the path section of URL. The optional safe parameter specifies additional ASCII characters that should not be quoted — its default value is '/'.
案例:
url="https://www.zhihu.com/question/50056807/answer/223566912"
print(urllib.parse.quote(url))
print(urllib.parse.quote(url,safe=":"))
输出:
https%3A//www.zhihu.com/question/50056807/answer/223566912
https:%2F%2Fwww.zhihu.com%2Fquestion%2F50056807%2Fanswer%2F223566912
3.2. quote_plus()
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ño/') yields '%2FEl+Ni%C3%B1o%2F'.
这个函数和quote()相似,但是这个函数能把空格转成加号,并且safe的默认值为空
3.3. quote_from_bytes()
urllib.parse.quote_from_bytes(bytes, safe=’/’)
和quote()相似,但是接收的是字节而不是字符;
笔记-urllib-parse的更多相关文章
- python学习笔记——urllib库中的parse
1 urllib.parse urllib 库中包含有如下内容 Package contents error parse request response robotparser 其中urllib.p ...
- 我与python3擦肩而过(三)—— 我去。。又是编码问题——urllib.parse.unquote
记得初学python时就学的爬虫,经常遇到编码问题(其实在python3里面编码问题已经很少了...),用requests库就挺方便解决这些问题的.近来有共同学习python的程序员写了个电子书网站, ...
- urllib.parse
1 url分解 import urllib.parse result = urllib.parse.urlparse('http://www.baidu.com') print(result) 结果为 ...
- urllib.parse.urlencode
urllib.request.urlopen(url,data,timeout) 其中如果data被赋值,则请求的方式就会由get转为post,而post需要提供一些待处理的数据. 这些待处理的数据需 ...
- Python 的 urllib.parse 库解析 URL
Python 中的 urllib.parse 模块提供了很多解析和组建 URL 的函数. 解析url urlparse() 函数可以将 URL 解析成 ParseResult 对象.对象中包含了六 ...
- python3中的urllib.parse的常用方法
将URL按一定的格式进行拆分 使用 urllib.parse.urlparse将url分为6个部分,返回一个包含6个字符串项目的元组:协议.位置.路径.参数.查询.片段 参照官方地址:https:// ...
- python3 urllib.parse 常用函数
1.获取url参数 urlparse from urllib import parse url = "https://docs.python.org/3.5/library/urllib.p ...
- urllib.parse.quote
sklearn实战-乳腺癌细胞数据挖掘(博主亲自录制视频) https://study.163.com/course/introduction.htm?courseId=1005269003& ...
- URL组成成分及各部分作用简介及urllib.parse / uri
URL的一般格式为(带方括号[]的为可选项): protocol :// hostname[:port] / path / [;parameters][?query]#fragment urllib. ...
- urllib.parse.parse_qsl 的一个小问题
最近在使用urllib时发现的一个问题,记录一下. 首先请分别执行下面这两句代码: 1."你好".encode("utf8").decode("gbk ...
随机推荐
- 如何下载最新的固件到Pixhawk
连接Pixhawk至电脑 当Mission Planner 已经安装至你的电脑上,使用micro USB数据线连接pixhawk到您的计算机上. 使用一个USB端口直接在您的计算机上,不要用USB集线 ...
- 从零开始的全栈工程师——js篇2.15(offsetLeft)
元素的属性 Div.attributes 是所有标签属性构成的数据集合 Div.classList 是所有class名构成的数组集合 在classList的原型链上看以看到add()和remove() ...
- MATLAB之画确定区域内不重合的随机圆
MATLAB之画确定区域内不重合的随机圆 程序要求:在确定区域内,画互不重合的圆. 知识点: (1)A=p'; 转置运算 (2)ones(a,b)产生a行b列全1数组 (3)rand(a,b)产生a行 ...
- 【extjs6学习笔记】1.9 初始: Mixins
Mixin允许我们使用一个类的函数作为另一个类的函数而不继承. Mixins可以使用mixins关键字定义,并将值指定为JSON对象,其中属性的名称应该是要使用的方法的名称,属性的值将是定义方法的类的 ...
- 关于HTML中时间格式以及查询数据库的问题
1.默认时间格式,加入属性dateFormate="yyyy-MM-dd" 2.设置默认值,value="2017-6-22" 3.在JavaScript中将获 ...
- 在github中的READEME中添加图片或者动图
在github中reademe中添加动图或者图片 将你需要展示的图片放在这个项目中的某个文件夹中,然后再reademe中这样引入 
只和连通分量以及度数有关.不同连通分量只要连一条边就够了,连通分量为0的时候要特判.一个连通分量只需看度数为奇的点的数量,两个端点(度数为奇)是必要的. 如果多了,奇点数也一定是2的倍数(一条边增加两 ...
- 2018.2.8 php实现qq登陆接口
PHP实现QQ登录的原理和实现过程 2018-02-08 学习与分享 PHP自学中心 第三方登录,就是使用大家比较熟悉的比如QQ.微信.微博等第三方软件登录自己的网站,这可以免去注册账号.快速留住用户 ...
- LigerUi中表(Grid)控件的相关属性笔记
http://blog.csdn.net/dxnn520/article/details/8216560 // ========================================= [每 ...