在urllib
或其他庫中是否存在一個簡單的方法來執行此任務? URL編碼將用不安全的ASCII字符替換爲後跟兩個十六進制數字的「%」。python中的URL編碼
下面是一個輸入的一個例子,我的預期輸出:
Mozilla/5.0 (Linux; U; Android 4.0; xx-xx; Galaxy Nexus Build/IFL10C) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30
Mozilla%2F5.0+%28Linux%3B+U%3B+Android+4.0%3B+xx-xx%3B+Galaxy+Nexus+Build%2FIFL10C%29+AppleWebKit%2F534.30+%28KHTML%2C+like+Gecko%29+Version%2F4.0+Mobile+Safari%2F534.30
或[urllib.quote_plus](http://docs.python.org/library/urllib.html#urllib.quote_plus),因爲OP需要'+'而不是'%20'。 – Avaris 2012-01-18 06:09:55
但爲了得到OP要求的內容,使用'urllib.quote_plus'。 – 2012-01-18 06:10:02