2010-06-14 137 views
2

我正在使用履帶。通常,當我在瀏覽器中輸入url1時,瀏覽器將其轉換爲url2。 我如何在Python中做到這一點?轉換履帶的網址

URL1:www.odevsitesi.com/ara.asp?kelime=doğanındengesininbozulması

URL2:www.odevsitesi.com/ara.asp?kelime=do%F0an%FDn%20dengesinin%20bozulmas%FD

回答

5

您需要對URL進行正確編碼(在您的案例中爲iso-8859-9),將它分成幾部分,urllib.quote查詢部分,然後重新組合。 I.e .:

>>> import urlparse 
>>> import urllib 
>>> x = u'http://www.odevsitesi.com/ara.asp?kelime=doğanın dengesinin bozulması' 
>>> y = x.encode('iso-8859-9') 
>>> # just to show what the split of y looks like (we can also handle it as a tuple): 
>>> urlparse.urlsplit(y) 
SplitResult(scheme='http', netloc='www.odevsitesi.com', path='/ara.asp', query='kelime=do\xf0an\xfdn dengesinin bozulmas\xfd', fragment='') 
>>> z = urlparse.urlsplit(y) 
>>> quoted = z[:3] + (urllib.quote(z.query), z.fragment) 
>>> # now just to show you what the 'quoted' tuple looks like: 
>>> quoted 
('http', 'www.odevsitesi.com', '/ara.asp', 'kelime%3Ddo%F0an%FDn%20dengesinin%20bozulmas%FD', '') 
>>> # and finally putting it back together: 
>>> urlparse.urlunsplit(quoted) 
'http://www.odevsitesi.com/ara.asp?kelime%3Ddo%F0an%FDn%20dengesinin%20bozulmas%FD' 
4

urllib.quote

http://docs.python.org/library/urllib.html#urllib.quote

實施例:quote('/~connolly/')產量'/%7econnolly/'

+0

謝謝你的回答。 但這不適合我。 >>> urllib.quote(「www.odevsitesi.com/ara.asp?kelime=doğanındengesininbozulması」) >>>'www.odevsitesi.com/ara.asp%3Fkelime%3Ddo%F0an%FDn% 20dengesinin%20bozulmas%FD' – user260223 2010-06-14 17:34:09

+1

只要確保你沒有使用unicode,哪個urllib在Python 3之前不支持的那個urllib – danben 2010-06-14 17:36:40

+2

@ user260223:你只想對url查詢字符串進行url編碼,而不是整個url – danben 2010-06-14 17:37:12