Python, Tutorials

URL shortener in Python

What is a URL- Shortener?

URL shoretner as the name says shortens the URL. URL shorteners are to shrink long website urls like “websitename.com/shop/men/jean/color” into something like “bitly.co/iuehsj“, this really helps when you are placing a link on sites like “Twitter” where word count is limited.

The URL-shortener not only shortens the url but it also helps to track the link activity and generate data about how many times the link was clicked, from where the link was clicked, from which site the link was clicked, and which links are performing better than other links. Different URL-shortener services can help you analyze different parameters of a link.

You can also use URL-shortner services to make custom short links using your top level domain and track them.

How to use URL Shortner API is Python.

import pyshortener

Pyshortener is a python library which provides support to connect with multiple URL-shortener services such as bit.ly, TinyURL, adf.ly, tiny.cc etc. to generate short-URL easily without logging in to there website again and again.

Python code for URL-shortner

First of all install pyshortner library using the command

pip install pyshorteners

create a object using pyshorteners.Shortener()

Use this pyshortener object to connect the url-shortener service and the function to perform.

For example-

link = pyshorteners.Shortener() creates the pyshortner object

link.tinyurl.short(url) link object uses the tinyurl short fucntion to generate short url of the long url passes as the argument

import pyshorteners

def shorten(url):
	link = pyshorteners.Shortener()
	return link.tinyurl.short(url)

if __name__ == "__main__":
	url = input("Enter link to shorten:")
	print(f"\n{shorten(url)}")

You can also check total number of click, and other parameter by using the pyshortner library.

To know more you can read references: https://pyshorteners.readthedocs.io/en/latest/


Thank you for reading, Happy Learning, drop your suggestion in the comments.

Feel free to follow us on Youtube, Linked In , Instagram