python URLObject url处理模块
1、需求来源
给一个url串,例如https://github.com/zacharyvoase/urlobject?spam=eggs#foo,想要截取串中某个部分,比如传输协议(https)、服务器名称、用户名密码、路径信息、后面query等。自己能想到的主要由以下几种方法:
(1)正则
(2)使用字符串处理函数
(3)使用urlobject模块
(4)使用urlparser模块
第一次接触urlobject,总结一下其使用方法。
2、urlobject安装
pip install urlobject
3、urlobject基础使用
urlobject的基本使用
(1){通过创建URLObject 来表示URL,URLObject 是unicode(Python3中是str)的普通子类。下面几种简单的方法可以获取想要获取的部分。
>>>from urlobject import URLOBject >>>url = URLObject("https://github.com/zacharyvoase/urlobject?spam=eggs#foo") >>> print(url) https://github.com/zacharyvoase/urlobject?spam=eggs#foo >>> print(url.scheme) #获取传输协议 https >>> print(url.netloc) #获取服务器主机,全网络地址,包括username,password,port等 github.com >>> print(url.hostname)#获取服务器主机 github.com >>> (url.username, url.password)#用户名、密码 (None, None) >>> print(url.port) #端口号 None >>> url.default_port 443 >>> print(url.path)#获取路径 /zacharyvoase/urlobject >>> print(url.query)#获取query spam=eggs >>> print(url.fragment) #获取fragment
(2)可以通过使用with_*()方法,替换任何想替换的部分,因为unicode是不可变的,当然URLObject也是如此,因此下面的方法不会改变原有的URLObject,而是会返回新的URLObject:
>>> print(url.with_scheme('http')) http://github.com/zacharyvoase/urlobject?spam=eggs#foo >>> print(url.with_netloc('example.com')) https://example.com/zacharyvoase/urlobject?spam=eggs#foo >>> print(url.with_auth('alice', '1234')) https://alice:1234@github.com/zacharyvoase/urlobject?spam=eggs#foo >>> print(url.with_path('/some_page')) https://github.com/some_page?spam=eggs#foo >>> print(url.with_query('funtimes=yay')) https://github.com/zacharyvoase/urlobject?funtimes=yay#foo >>> print(url.with_fragment('example')) https://github.com/zacharyvoase/urlobject?spam=eggs#example
(3)对于url中的query来说,可以使用without_方法:
>>> print(url.without_query()) https://github.com/zacharyvoase/urlobject#foo >>> print(url.without_fragment())
https://github.com/zacharyvoase/urlobject?spam=eggs
4、relative函数相对URL的处理
针对某 一 url,例如https://github.com/zacharyvoase/urlobject?spam=eggs#foo,如果只想换掉urlobject?spam=eggs#foo,或者换掉zacharyvoase/urlobject?spam=eggs#foo。可以通过relative函数来处理
>>> print(url.relative('another-project')) https://github.com/zacharyvoase/another-project >>> print(url.relative('?different-query-string')) https://github.com/zacharyvoase/urlobject?different-query-string >>> print(url.relative('#frag')) https://github.com/zacharyvoase/urlobject?spam=eggs#frag
如果relative参数为完全的url则,返回全新的url
>>> print(url.relative('http://example.com/foo')) http://example.com/foo
根据自己的需求可以任意设置相对路径的起始位置来满足需求
>>> print(url.relative('//example.com/foo')) https://example.com/foo >>> print(url.relative('/dvxhouse/intessa')) https://github.com/dvxhouse/intessa >>> print(url.relative('/dvxhouse/intessa?foo=bar')) https://github.com/dvxhouse/intessa?foo=bar >>> print(url.relative('/dvxhouse/intessa?foo=bar#baz')) https://github.com/dvxhouse/intessa?foo=bar#baz
5、Path
url的path属性其实是一个URLPath对象,有一下方法和属性,来处理path
>>> print(url.path) /zacharyvoase/urlobject >>> print(url.path.parent) /zacharyvoase/ >>> print(url.path.segments) ('zacharyvoase', 'urlobject') >>> print(url.path.add_segment('subnode')) /zacharyvoase/urlobject/subnode >>> print(url.path.root)
这些方法同样适用于URLObject,同样将会返回全心的URLObject对象,而不是对原有url进行更改,如下:
>>> print(url.parent) https://github.com/zacharyvoase/?spam=eggs#foo >>> print(url.add_path_segment('subnode')) https://github.com/zacharyvoase/urlobject/subnode?spam=eggs#foo >>> print(url.add_path('tree/urlobject2')) https://github.com/zacharyvoase/urlobject/tree/urlobject2?spam=eggs#foo >>> print(url.root) https://github.com/?spam=eggs#foo
6、针对url串中query的处理
URLObject中的query属性是一个QueryString对象,可以调用对象中的方法对query进行处理
>>> print(url.query) spam=eggs >>> url.query.list # aliased as url.query_list [('spam', 'eggs')] >>> url.query.dict # aliased as url.query_dict {'spam': 'eggs'} >>> url.query.multi_dict # aliased as url.query_multi_dict {'spam': ['eggs']}
在query基础上修改也是比较简单的,可以add或者setquery参数,以add开头的,可以设置某个key为多个value:
>>> print(url.query.add_param('spam', 'ham')) spam=eggs&spam=ham
而以set为开头的函数,会使某个key只有一个value
>>> print(url.query.set_param('spam', 'ham')) spam=ham
参数可以是一个字典
>>> print(url.query.add_params({'spam': 'ham', 'foo': 'bar'})) spam=eggs&foo=bar&spam=ham >>> print(url.query.set_params({'spam': 'ham', 'foo': 'bar'})) foo=bar&spam=ham
可以使用del_param()或者del_params()删除query中的参数
>>> print(url.query.del_param('spam')) # Result is empty >>> print(url.query.add_params({'foo': 'bar', 'baz': 'blah'}).del_params(['spam', 'foo'])) baz=blah
上面针对query对象的方法,是URLObject对象的方法的别名,可以直接在URLObject对象上直接操作,其实是调用的同一段代码:
>>> print(url.add_query_param('spam', 'ham')) https://github.com/zacharyvoase/urlobject?spam=eggs&spam=ham#foo >>> print(url.set_query_param('spam', 'ham')) https://github.com/zacharyvoase/urlobject?spam=ham#foo >>> print(url.del_query_param('spam')) https://github.com/zacharyvoase/urlobject#foo
7、总结
具体的URLObject类的函数api 具体参考这个网址:https://urlobject.readthedocs.io/en/latest/api.html