当前位置:   article > 正文

python+pyshorteners—长短链接(网址)转换_pyshorteners库

pyshorteners库

目录

长短链接转换

长网址转短网址


长短链接转换

  1. import pyshorteners as psn
  2. import requests
  3. def longurl_to_shorturl(longurl):
  4. # 长链接转短链接
  5. shorturl = psn.Shortener().chilpit.short(url)
  6. return shorturl
  7. def shorturl_to_longurl(shorturl):
  8. # 短链接转长链接
  9. res = requests.head(shorturl)
  10. return res.headers.get('Location')
  11. if __name__ == '__main__':
  12. url = 'https://hanyu.baidu.com/shici/detail?from=kg1&highlight=%E6%80%A8%E7%9B%9B%E5%B9%B4%E4%B9%8B%E8%8E%AB%E5%BD%93%E3%80%82&pid=dabf9384e3ff4fe8bc7b9f64385c1015&srcid=51369'
  13. short_url = longurl_to_shorturl(url)
  14. print(f'长链接转为短链接结果:{short_url}')
  15. long_url = shorturl_to_longurl(short_url)
  16. print(f'短链接转为长链接结果:{long_url}')

  

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/码创造者/article/detail/810047
推荐阅读
相关标签
  

闽ICP备14008679号