javascript - 像javascript一样在python中递归解码URI组件

标签 javascript python python-3.x python-2.7 urllib2

我有一个编码的 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 中得到相同的结果。 我尝试了以下方法:

print urllib2.unquote(urllib2.unquote(urllib2.unquote("http://www.yelp.com/biz/carriage-house-caf%25C3%25A9-houston-2").decode("utf-8")))

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

我正在使用python2.7.3,谁能帮帮我?

最佳答案

我想一个简单的循环就可以解决问题:

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'

关于javascript - 像javascript一样在python中递归解码URI组件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14702231/

相关文章:

javascript - 碰撞canvas html 5时球碰撞粘在一起

javascript - 从 span 的 innerHTML 获取 `selectionStart`

javascript - Colorbox 模态不调整大小

python - 如何使用 python 网络爬虫获取 HTML 子类的文本?输出似乎是一个空数组

python-3.x - Seaborn 中分组箱线图的单独颜色

javascript - 扩展工具栏项目监听器在溢出菜单中不起作用

python - 使用python比较文本行(多个列表)

python - 将卷积操作应用于图像 - PyTorch

python - 转换mmap对象(mmap不支持串联)/将c代码转换为python

python - 如何确定通过 `from m import *` 导入的内容?