urlparse.urlparse在方案之后返回3'/'而不是2 [英] urlparse.urlparse returning 3 '/' instead of 2 after scheme
本文介绍了urlparse.urlparse在方案之后返回3'/'而不是2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果缺少,我想在给定的URL字符串前添加'http'方案名称.否则,请不要使用url,所以我认为urlparse是执行此操作的正确方法.但是,只要没有方案,并且我使用get url,我就会在方案和域之间得到///而不是'//'.
I'd like to add the 'http' scheme name in front of a given url string if it's missing. Otherwise, leave the url alone so I thought urlparse was the right way to do this. But whenever there's no scheme and I use get url, I get /// instead of '//' between the scheme and domain.
>>> t = urlparse.urlparse('www.example.com', 'http')
>>> t.geturl()
'http:///www.example.com' # three ///
如何转换此网址,使其看起来像这样:
How do I convert this url so it actually looks like:
'http://www.example.com' # two //
推荐答案
简短的回答(但这有点重言自语):
Short answer (but it's a bit tautological):
>>> urlparse.urlparse("http://www.example.com").geturl()
'http://www.example.com'
在您的示例代码中,主机名被解析为路径而不是网络位置:
In your example code, the hostname is parsed as a path not a network location:
>>> urlparse.urlparse("www.example.com/go")
ParseResult(scheme='', netloc='', path='www.example.com/go', params='', \
query='', fragment='')
>>> urlparse.urlparse("http://www.example.com/go")
ParseResult(scheme='http', netloc='www.example.com', path='/go', params='', \
query='', fragment='')
这篇关于urlparse.urlparse在方案之后返回3'/'而不是2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文