我正在使用此代碼在 python 3 中取消縮短網址,但代碼按原樣返回網址(已縮短),那么我該怎么做才能使其取消縮短?import requestsimport http.clientimport 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
1 回答

千巷貓影
TA貢獻1829條經驗 獲得超7個贊
在 python3response.status/100 == 3
中將True
僅用于狀態代碼 300。對于任何其他 3xx 代碼,它將是False
. 改用樓層劃分response.status//100 == 3
或其他方式來測試重定向代碼。
編輯:看起來您正在使用@Aybars 發布的 SO 問題中的代碼,并且代碼段頂部有注釋,在 python3 中要做什么。另外,最好能提及代碼的來源。
添加回答
舉報
0/150
提交
取消