赞
踩
安装python第三方库pyshorteners
URL长转短:输入待转换的URL,运行代码实现URL转短,输出结果为短链接
本次代码运行使用jupyter notebook(anaconda) ,python 3.9.13
步骤1:运行代码,成功安装
pip install pyshorteners
步骤2:试运行代码,成功后可替换待转换url再次运行
- #长链接转短,使用tinyurl
- from pyshorteners import Shortener
-
- short_engine = Shortener()
-
- res = short_engine.tinyurl.short('https://blog.csdn.net/hao20211228/article/details/128801798?csdn_share_tail=%7B%22type%22%3A%22blog%22%2C%22rType%22%3A%22article%22%2C%22rId%22%3A%22128801798%22%2C%22source%22%3A%22hao20211228%22%7D')
- print(res)
运行代码结果截图:
3.短链接还原出长链接
输入url短链接,运行代码可还原出原链接
- import requests
-
- url = "https://tinyurl.com/2kzqz9k7"
- res = requests.head(url)
- print(res.headers.get('location'))
运行结果截图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。