类python javascrip中URI组件的递归解码

2024-06-01 11:39:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个编码的URI组件"http://www.yelp.com/biz/carriage-house-caf%25C3%25A9-houston-2"。我可以通过递归地应用decodeURIComponent函数将其转换为"http://www.yelp.com/biz/carriage-house-café-houston-2",如下所示

function recursiveDecodeURIComponent(uriComponent){
        try{
            var decodedURIComponent = decodeURIComponent(uriComponent);
            if(decodedURIComponent == uriComponent){
                return decodedURIComponent;
            }
            return recursiveDecodeURIComponent(decodedURIComponent);
        }catch(e){
            return uriComponent;
        }
    }
    console.log(recursiveDecodeURIComponent("http://www.yelp.com/biz/carriage-house-caf%25C3%25A9-houston-2"))

输出:"http://www.yelp.com/biz/carriage-house-café-houston-2"。在

我想在python中得到同样的结果。 我尝试了以下方法:

^{pr2}$

但我得到了http://www.yelp.com/biz/carriage-house-café-houston-2。我得到了'é',而不是预期的字符é,而不管调用urllib2.unquote的次数是多少。在

我在用python2.7.3,有人能帮我吗?在


Tags: comhttp编码returnwwwhouseyelpcaf
1条回答
网友
1楼 · 发布于 2024-06-01 11:39:31

我想一个简单的循环就可以做到:

uri = "http://www.yelp.com/biz/carriage-house-caf%25C3%25A9-houston-2"

while True:
    dec = urllib2.unquote(uri)
    if dec == uri:
        break
    uri = dec

uri = uri.decode('utf8')
print '%r' % uri  
# u'http://www.yelp.com/biz/carriage-house-caf\xe9-houston-2'

相关问题 更多 >