Python: Convertir los TinyURL (bit.ly, TinyURL, ow.ly) a una URL completa
Pregunta
Estoy aprendiendo Python y está interesado en cómo esto se puede lograr. Durante la búsqueda de la respuesta, me encontré con este servicio: http://www.longurlplease.com
Por ejemplo:
http://bit.ly/rgCbf se puede convertir a:
http://webdesignledger.com/ regalos / las-best-social-media-iconos-todo-en-uno-lugar
He hecho un poco de inspeccionar con Firefox y veo que la URL original no está en la cabecera.
Solución
urllib2
, que ofrece la forma más fácil de hacer esto:
>>> import urllib2
>>> fp = urllib2.urlopen('http://bit.ly/rgCbf')
>>> fp.geturl()
'http://webdesignledger.com/freebies/the-best-social-media-icons-all-in-one-place'
Por el amor de referencia, sin embargo, tenga en cuenta que esto también es posible con httplib
:
>>> import httplib
>>> conn = httplib.HTTPConnection('bit.ly')
>>> conn.request('HEAD', '/rgCbf')
>>> response = conn.getresponse()
>>> response.getheader('location')
'http://webdesignledger.com/freebies/the-best-social-media-icons-all-in-one-place'
PycURL
, aunque no estoy seguro si esto es la mejor manera de hacerlo es utilizar :
>>> import pycurl
>>> conn = pycurl.Curl()
>>> conn.setopt(pycurl.URL, "http://bit.ly/rgCbf")
>>> conn.setopt(pycurl.FOLLOWLOCATION, 1)
>>> conn.setopt(pycurl.CUSTOMREQUEST, 'HEAD')
>>> conn.setopt(pycurl.NOBODY, True)
>>> conn.perform()
>>> conn.getinfo(pycurl.EFFECTIVE_URL)
'http://webdesignledger.com/freebies/the-best-social-media-icons-all-in-one-place'
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow