简介
yarl 便于 URL 解析和修改
安装
pip install yarl
初试
http://user:pass@example.com:8042/over/there?name=ferret#nose
\__/ \__/ \__/ \_________/ \__/\_________/ \_________/ \__/
| | | | | | | |
scheme user password host port path query fragment
from yarl import URL
url = URL('https://www.python.org/~guido?arg=1#frag')
print(url) # URL('https://www.python.org/~guido?arg=1#frag')
print(url.scheme) # https
print(url.host) # www.python.org
print(url.path) # /~guido
print(url.query_string) # arg=1
print(url.query) # <MultiDictProxy('arg': '1')>
print(url.fragment) # frag
print(url.parts) # ('/', '~guido')
print(URL('http://example.com:8080').explicit_port) # 8080
print(URL('http://example.com/path/to.txt').suffix) # .txt
更多属性查阅 URL properties
操作URL
/
:连路径%
:加参数- 字符串会自动编码
url.human_repr()
:人类可读
from yarl import URL
url = URL('https://www.python.org/~guido?arg=1#frag')
print(url.parent / 'downloads/source') # URL('https://www.python.org/downloads/source')
url = URL('https://www.python.org')
print(url / 'foo' / 'bar') # URL('https://www.python.org/foo/bar')
print(url / 'foo' % {'bar': 'baz'}) # URL('https://www.python.org/foo?bar=baz')
url = URL('https://www.python.org/你好')
print(url) # URL('https://www.python.org/%E4%BD%A0%E5%A5%BD')
print(url.human_repr()) # URL('https://www.python.org/你好')
绝对和相对URL
from yarl import URL
print(URL('http://example.com').is_absolute()) # True
print(URL('//example.com').is_absolute()) # True
print(URL('/path/to').is_absolute()) # False
print(URL('path').is_absolute()) # False
生成URL
query
和query_string
只能传一个build()
相当于__init__
with_scheme('https')
:替换 schemewith_user(user)
:替换 Userwith_password(password)
:替换 passwordwith_host(host)
:替换 hostwith_port(port)
:替换 portwith_path(path)
:替换 pathwith_query(query)
:替换 querywith_fragment(fragment)
:替换 fragmentwith_name(name)
:替换 namewith_suffix(suffix)
:替换 suffixparent
:获取父节点origin()
:原始 URLrelative()
:相对 URLjoin(url)
:拼接 URL
from yarl import URL
print(URL.build()) # URL('')
print(URL.build(scheme='http', host='example.com')) # URL('http://example.com')
print(URL.build(scheme='http', host='example.com', query={'a': 'b'})) # URL('http://example.com/?a=b')
print(URL.build(scheme='http', host='example.com', query_string='a=b')) # URL('http://example.com/?a=b')
print(URL('http://example.com/path/to').with_host('python.org')) # URL('http://python.org/path/to')
print(URL('http://example.com:8888').with_port(9999)) # URL('http://example.com:9999')
print(URL('http://example.com:8888').with_port(None)) # URL('http://example.com')
print(URL('http://example.com/').with_path('/path/to')) # URL('http://example.com/path/to')
print(URL('http://example.com/path?a=b').with_query('c=d')) # URL('http://example.com/path?c=d')
print(URL('http://example.com/path?a=b').with_query({'c': 'd'})) # URL('http://example.com/path?c=d')
print(URL('http://example.com/path?a=b').with_query({'c': [1, 2]})) # URL('http://example.com/path?c=1&c=2')
print(URL('http://example.com/path?a=b').with_query(None)) # URL('http://example.com/path')
print(URL('http://example.com/path?a=b&b=1').with_query(b='2')) # URL('http://example.com/path?b=2')
print(URL('http://example.com/path?a=b&b=1').with_query([('b', '2')])) # URL('http://example.com/path?b=2')
print(URL('http://example.com/path?a=b').update_query('c=d')) # URL('http://example.com/path?a=b&c=d')
print(URL('http://example.com/path?a=b').update_query({'c': 'd'})) # URL('http://example.com/path?a=b&c=d')
print(URL('http://example.com/path?a=b').update_query({'c': [1, 2]})) # URL('http://example.com/path?a=b&c=1&c=2')
print(URL('http://example.com/path?a=b&b=1').update_query(b='2')) # URL('http://example.com/path?a=b&b=2')
print(URL('http://example.com/path?a=b&b=1').update_query([('b', '2')])) # URL('http://example.com/path?a=b&b=2')
print(URL('http://example.com/path?a=b&c=e&c=f').update_query(c='d')) # URL('http://example.com/path?a=b&c=d')
print(URL('http://example.com/path?a=b').update_query('c=d&c=f')) # URL('http://example.com/path?a=b&c=d&c=f')
print(URL('http://example.com/path?a=b') % {'c': 'd'}) # URL('http://example.com/path?a=b&c=d')
print(URL('http://example.com/path/to?arg#frag').with_name('new')) # URL('http://example.com/path/new')
print(URL('http://example.com/path/to?arg#frag').parent) # URL('http://example.com/path')
print(URL('http://example.com/path/to?arg#frag').origin()) # URL('http://example.com')
print(URL('http://user:pass@example.com/path').origin()) # URL('http://example.com')
print(URL('http://example.com/path/to?arg#frag').relative()) # URL('/path/to?arg#frag')
base = URL('http://example.com/path/index.html') # URL('http://python.org/page.html')
print(base.join(URL('page.html')))
是否为默认端口
Scheme | Port |
---|---|
http | 80 |
https | 443 |
ws | 80 |
wss | 443 |
from yarl import URL
print(URL('http://example.com').is_default_port()) # True
print(URL('http://example.com:80').is_default_port()) # True
print(URL('http://example.com:8080').is_default_port()) # False
print(URL('/path/to').is_default_port()) # False