使用python扩展一点点林

2024-04-25 20:42:05 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用Python获取一点点链接并返回完全展开的链接?在

如果输入的链接不是一点点链接,应返回原始链接。在


Tags: 链接
3条回答

Python 2:

>>> import urllib2
>>> print urllib2.urlopen('http://bit.ly/1cPIdPg').url
http://stackoverflow.com/

您也可以使用geturl()方法:

^{pr2}$

对于Python 3:

>>> from urllib.request import urlopen
>>> print(urlopen('http://bit.ly/1cPIdPg').geturl())
http://stackoverflow.com/

可以使用requestsPython库来完成。下面是代码

import requests
r = requests.get('http_tiny_url_for_stackoverflow_or_any')
print r.url

输出:

^{pr2}$

您可以使用urllib模块执行此操作

import urllib
response = urllib.urlopen('http://bit.ly/1mlEbqY')
print response.url

输出:

^{pr2}$

相关问题 更多 >