在 python 中取消缩短 url 3

Unshorten url in python 3

我正在使用此代码来缩短 python 3 中的 urls,但是代码 returns 和 url 原样(缩短),所以应该怎么办我这样做是为了让它不被缩短吗?

import requests
import http.client
import urllib.parse as urlparse   

def unshortenurl(url):
    parsed = urlparse.urlparse(url) 
    h = http.client.HTTPConnection(parsed.netloc) 
    h.request('HEAD', parsed.path) 
    response = h.getresponse() 
    if response.status/100 == 3 and response.getheader('Location'):
        return response.getheader('Location') 
    else: return url

在 python3 中,response.status/100 == 3 仅对于状态代码 300 为 True。对于任何其他 3xx 代码,它将为 False。请改用楼层划分 response.status//100 == 3 或其他方式来测试重定向代码。

编辑:看起来您正在使用 SO question posted by @Aybars 中的代码,并且在片段顶部有评论在 python3 中做什么。另外,最好能提到代码的来源。