【发布时间】:2019-05-12 04:01:40
【问题描述】:
我在 python 3 中使用此代码来缩短 url,但代码返回原样(缩短)的 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
【问题讨论】:
-
我正在使用这个确切的代码,但它不起作用
-
if 300 <= response.status < 400在这里会更好更清晰!
标签: python python-3.x url urllib3 http.client