作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当netloc为空时urlparse.urlunparse不一致:
>>> urlparse.urlunparse(('http','','test_path', None, None, None))
'http:///test_path'
>>> urlparse.urlunparse(('ftp','','test_path', None, None, None))
'ftp:///test_path'
>>> urlparse.urlunparse(('ssh','','test_path', None, None, None))
'ssh:test_path'
这是错误还是功能?我希望 urlunparse 始终如第一个示例所示,即使无法识别方案也是如此。
最佳答案
您传递给 urlunparse
的 data
元组具有以下组件:
scheme, netloc, url, query, fragment = data
当没有netloc
,并且scheme
不在uses_netloc
中时,url为
url = scheme + ':' + url
这就是 urlunparse(调用 urlunsplit)的方式 is defined :
def urlunsplit(data):
...
scheme, netloc, url, query, fragment = data
if netloc or (scheme and scheme in uses_netloc and url[:2] != '//'):
if url and url[:1] != '/': url = '/' + url
url = '//' + (netloc or '') + url
if scheme:
url = scheme + ':' + url
请注意 'ssh'
不在 uses_netloc
中:
uses_netloc = ['ftp', 'http', 'gopher', 'nntp', 'telnet',
'imap', 'wais', 'file', 'mms', 'https', 'shttp',
'snews', 'prospero', 'rtsp', 'rtspu', 'rsync', '',
'svn', 'svn+ssh', 'sftp','nfs','git', 'git+ssh']
如果您提供 netloc
,您会得到一个以 ssh://
开头的 url:
In [140]: urlparse.urlunparse(('ssh','netloc','test_path', None, None, None))
Out[140]: 'ssh://netloc/test_path'
关于python - 为什么 urlparse.urlenparse 工作不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15514306/
当netloc为空时urlparse.urlunparse不一致: >>> urlparse.urlunparse(('http','','test_path', None, None, None))
我是一名优秀的程序员,十分优秀!