如何将%3A和%2F转换为:和/在python中的url?

时间:2016-08-06 06:18:33

标签: python python-3.x url

如何在网址中转换替换(%3A和%2F ...)。
网址
https://url/login_data.php?username=user&categoryid=0&URL=https%3A%2F%2Furl%2F%26TIME%3DFri%2520Aug%252005%25202016%252011%3A40%3A14%2520GMT%2B0530%28India%2520Standard%2520Time%29

必填网址
https://url/login_data.php?username=user&categoryid=0&URL=https://url/&TIME=Sat Aug 06 2016 11:42:36 GMT+0530 (India Standard Time)

我想知道有没有简单的方法呢?

3 个答案:

答案 0 :(得分:4)

python 2.7中,使用urllib.unquote

>>> import urllib
>>> urllib.unquote(urllib.unquote('https://url/login_data.php?username=user&categoryid=0&URL=https%3A%2F%2Furl%2F%26TIME%3DFri%2520Aug%252005%25202016%252011%3A40%3A14%2520GMT%2B0530%28India%2520Standard%2520Time%29'))
'https://url/login_data.php?username=user&categoryid=0&URL=https://url/&TIME=Fri Aug 05 2016 11:40:14 GMT+0530(India Standard Time)'

python 3+中,使用urllib.parse.unquote

>>> from urllib.parse import unquote
>>> unquote(unquote("https://url/login_data.php?username=user&categoryid=0&URL=https%3A%2F%2Furl%2F%26TIME%3DFri%2520Aug%252005%25202016%252011%3A40%3A14%2520GMT%2B0530%28India%2520Standard%2520Time%29"))
'https://url/login_data.php?username=user&categoryid=0&URL=https://url/&TIME=Fri Aug 05 2016 11:40:14 GMT+0530(India Standard Time)'

答案 1 :(得分:1)

看看urllib.parse.unquote:“用他们的单字符等效替换%xx转义。”

答案 2 :(得分:0)

对于python3,

import urllib.parse
urllib.parse.unquote_plus("https://url/login_data.php?username=user&categoryid=0&URL=https%3A%2F%2Furl%2F%26TIME%3DFri%2520Aug%252005%25202016%252011%3A40%3A14%2520GMT%2B0530%28India%2520Standard%2520Time%29")