来自“urllib.parse”和“pathlib”的面向对象URL`

urlpath的Python项目详细描述


https://img.shields.io/travis/chrono-meter/urlpath.svghttps://img.shields.io/pypi/v/urlpath.svghttps://img.shields.io/pypi/l/urlpath.svg

依赖关系

安装

pip install urlpath

示例

导入:

>>> from urlpath import URL

创建对象:

>>> url = URL(
...     'https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment')

表示:

>>> url
URL('https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> print(url)
https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment
>>> url.as_uri()
'https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment'
>>> url.as_posix()
'https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment'

访问兼容属性:

>>> url.drive
'https://username:password@secure.example.com:1234'
>>> url.root
'/'
>>> url.anchor
'https://username:password@secure.example.com:1234/'
>>> url.path
'/path/to/file.ext'
>>> url.name
'file.ext'
>>> url.suffix
'.ext'
>>> url.suffixes
['.ext']
>>> url.stem
'file'
>>> url.parts
('https://username:password@secure.example.com:1234/', 'path', 'to', 'file.ext')
>>> url.parent
URL('https://username:password@secure.example.com:1234/path/to')

访问方案:

>>> url.scheme
'https'

访问netloc:

>>> url.netloc
'username:password@secure.example.com:1234'
>>> url.username
'username'
>>> url.password
'password'
>>> url.hostname
'secure.example.com'
>>> url.port
1234

访问查询:

>>> url.query
'field1=1&field2=2&field1=3'
>>> url.form_fields
(('field1', '1'), ('field2', '2'), ('field1', '3'))
>>> url.form
<FrozenMultiDict {'field1': ('1', '3'), 'field2': ('2',)}>
>>> url.form.get_one('field1')
'1'
>>> url.form.get_one('field3') is None
True

访问片段:

>>> url.fragment
'fragment'

路径操作:

>>> url / 'suffix'
URL('https://username:password@secure.example.com:1234/path/to/file.ext/suffix')
>>> url / '../../rel'
URL('https://username:password@secure.example.com:1234/path/to/file.ext/../../rel')
>>> (url / '../../rel').resolve()
URL('https://username:password@secure.example.com:1234/path/rel')
>>> url / '/'
URL('https://username:password@secure.example.com:1234/')
>>> url / 'http://example.com/'
URL('http://example.com/')

更换部件:

>>> url.with_scheme('http')
URL('http://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> url.with_netloc('www.example.com')
URL('https://www.example.com/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> url.with_userinfo('joe', 'pa33')
URL('https://joe:pa33@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> url.with_hostinfo('example.com', 8080)
URL('https://username:password@example.com:8080/path/to/file.ext?field1=1&field2=2&field1=3#fragment')
>>> url.with_fragment('new fragment')
URL('https://username:password@secure.example.com:1234/path/to/file.ext?field1=1&field2=2&field1=3#new fragment')
>>> url.with_components(username=None, password=None, query='query', fragment='frag')
URL('https://secure.example.com:1234/path/to/file.ext?query#frag')

替换查询:

>>> url.with_query({'field3': '3', 'field4': [1, 2, 3]})
URL('https://username:password@secure.example.com:1234/path/to/file.ext?field3=3&field4=1&field4=2&field4=3#fragment')
>>> url.with_query(field3='3', field4=[1, 2, 3])
URL('https://username:password@secure.example.com:1234/path/to/file.ext?field3=3&field4=1&field4=2&field4=3#fragment')
>>> url.with_query('query')
URL('https://username:password@secure.example.com:1234/path/to/file.ext?query#fragment')
>>> url.with_query(None)
URL('https://username:password@secure.example.com:1234/path/to/file.ext#fragment')

执行http请求:

>>> url = URL('https://httpbin.org/get')
>>> url.get()
<Response [200]>

>>> url = URL('https://httpbin.org/post')
>>> url.post(data={'key': 'value'})
<Response [200]>

>>> url = URL('https://httpbin.org/delete')
>>> url.delete()
<Response [200]>

>>> url = URL('https://httpbin.org/patch')
>>> url.patch(data={'key': 'value'})
<Response [200]>

>>> url = URL('https://httpbin.org/put')
>>> url.put(data={'key': 'value'})
<Response [200]>

监狱:

>>> root = 'http://www.example.com/app/'
>>> current = 'http://www.example.com/app/path/to/content'
>>> url = URL(root).jailed / current
>>> url / '/root'
JailedURL('http://www.example.com/app/root')
>>> (url / '../../../../../../root').resolve()
JailedURL('http://www.example.com/app/root')
>>> url / 'http://localhost/'
JailedURL('http://www.example.com/app/')
>>> url / 'http://www.example.com/app/file'
JailedURL('http://www.example.com/app/file')

尾部分隔符将保留:

>>> url = URL('http://www.example.com/path/with/trailing/sep/')
>>> str(url).endswith('/')
True
>>> url.trailing_sep
'/'
>>> url.name
'sep'
>>> url.path
'/path/with/trailing/sep/'
>>> url.parts[-1]
'sep'

>>> url = URL('http://www.example.com/path/without/trailing/sep')
>>> str(url).endswith('/')
False
>>> url.trailing_sep
''
>>> url.name
'sep'
>>> url.path
'/path/without/trailing/sep'
>>> url.parts[-1]
'sep'

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
junit cucumber为什么会找到“runTest.java”来运行测试?   在Eclipse中找不到java KeyPairGenerator   java NotSerializableException即使在实现Serializable之后   noclassdeffounderror(java字符串连接)为什么会出现这种异常?   java Guice:将接口绑定到由动态代理创建的实例   使用Spring数据neo4j创建空间索引时发生java错误   java对于需要在50多个excel文件上运行并且每个文件平均包含25k行的项目,最佳的方法是什么   javaNIO中的java缓冲区写入/发送消息问题   如何在Java/eclipse中添加不调用super()的警告   JavaSpring:mvcUrl映射错误的id   java应该在getInstance或构造函数中使用Init方法吗?   安卓中的java空指针异常错误   java Jsoup不能完全获取原始html代码