我正在学习python,对如何做到这一点很感兴趣。在搜索答案的过程中,我偶然发现了这个服务:http://www.longurlplease.com
例如:
http://bit.ly/rgCbf可以转换为:
http://webdesignledger.com/freebies/the-best-social-media-icons-all-in-one-place
我用Firefox做了一些检查,发现原始的url不在标题中。
发布于 2009-04-14 16:17:57
进入urllib2,它提供了执行此操作的最简单方法:
>>> import urllib2
>>> fp = urllib2.urlopen('http://bit.ly/rgCbf')
>>> fp.geturl()
'http://webdesignledger.com/freebies/the-best-social-media-icons-all-in-one-place'但是,为了便于参考,请注意,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,尽管我不确定这是否是使用它的最好方法:
>>> 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'https://stackoverflow.com/questions/748324
复制相似问题