【发布时间】:2018-01-21 15:39:41
【问题描述】:
我在扩展短 URL 时遇到问题,因为并非所有我使用的重定向都使用相同的重定向:
这个想法是扩展缩短的 url:这里有几个短 url 的例子 --> 最终 url。我需要一个函数来获取缩短的 url 并返回扩展的 url
http://chollo.to/675za --> http://www.elcorteingles.es/limite-48-horas/equipaje/?sorting=priceAsc&aff_id=2118094&dclid=COvjy8Xrz9UCFeMi0wod4ZULuw
所以我有一些半工作的东西,它在一些上面的例子中失败了
import requests
import httplib
import urlparse
def unshorten_url(url):
try:
parsed = urlparse.urlparse(url)
h = httplib.HTTPConnection(parsed.netloc)
h.request('HEAD', parsed.path)
response = h.getresponse()
if response.status / 100 == 3 and response.getheader('Location'):
url = requests.get(response.getheader('Location')).url
print url
return url
else:
url = requests.get(url).url
print url
return url
except Exception as e:
print(e)
【问题讨论】:
-
你在上面的例子中遇到了什么错误?
-
我得到了一个中间网络web.epartner.es/…