2016-08-26 9 views
0

pythonでは、が最も速いタイプのURLを "スキーム相対"または "プロトコル相対" URIに変換する方法?URIをスキーム相対URI(Pythonで)に変換する最速の方法


現在、私はthisのバージョンをやっているが、これを達成するために、より簡潔な/より高速な方法があるかもしれない考えていました。入力には常にhttps://またはhttp://が付加されています。

+1

あなたはその答えのバージョンをやっています....どのバージョン/どのように実装しているのかわかるようにコードを表示できます – depperm

答えて

1

です:

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 

私は見つけることができる最速の方法はstr.partitionである...私はおそらくそれを行うだろうか

+0

'scheme'と' netloc'の両方を取り除けないでしょうか? –

+0

nope ...それらはurlparseオブジェクトにあります –

関連する問題