2016-08-26 93 views
0

在python中,什么是最快的方式将scheme://netloc/path;parameters?query#fragment类型的url转换为“scheme relative”或“protocol relative”URI?将URI转换为方案相对URI的最快方法(在python中)


目前,我正在做一个版本的this,但认为可能是实现这一点更简洁/更快的方式。输入始终有https://http://附加到它。

+1

你正在做一个答案的版本....你可以显示你的代码,所以我们知道什么版本/你是如何实现它 – depperm

回答

1

,我能找到的最快方法是str.partition

In [1]: url = 'https://netloc/path;parameters?query#fragment' 

In [2]: %timeit url.partition('://')[2] 
The slowest run took 6.70 times longer than the fastest. This could mean that an intermediate result is being cached. 
1000000 loops, best of 3: 251 ns per loop 

In [3]: %timeit url.split('://', 1)[1] 
The slowest run took 5.20 times longer than the fastest. This could mean that an intermediate result is being cached. 
1000000 loops, best of 3: 407 ns per loop 

In [4]: %timeit url.replace('http', '', 1).replace('s://', '', 1) 
The slowest run took 4.24 times longer than the fastest. This could mean that an intermediate result is being cached. 
1000000 loops, best of 3: 589 ns per loop 

因为所有你正在做的是剥离文字的结局在一个固定的字符串中,解析URL似乎没有多大好处。

0
from urlparse import urlparse 
o = urlparse('http://www.cwi.nl:80/%7Eguido/Python.html') 

print o.scheme 
print o.netloc 
print o.path 

是我怎么可能会做...

+0

不会摆脱'计划'和'netloc'都? –

+0

nope ...它们位于urlparse对象中 –