Tim*_*ung 13 python tinyurl bit.ly
我只是学习python,并对如何实现这一点感兴趣.在寻找答案的过程中,我遇到了这项服务:http://www.longurlplease.com
例如:
http://bit.ly/rgCbf可以转换为:
http://webdesignledger.com/freebies/the-best-social-media-icons-all-in-one-place
我做了一些用Firefox检查,看到原始网址不在标题中.
Pao*_*ino 33
输入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'
Run Code Online (Sandbox Code Playgroud)
但是,为了参考,请注意,这也可以通过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'
Run Code Online (Sandbox Code Playgroud)
并用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'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7311 次 |
| 最近记录: |