URL Shortener in Python

Today we build URL Shortener in Python. As you know URL shortener help to reduce the length of the URL so that it can be shared easily on platforms like Twitter, where a number of characters are an issue.

There are so many URL Shorteners available in the market today, that will definitely help you out to solve the purpose. But today we build url shortner in python.

To do this we are using pyshorteners python module. pyshorteners is a Python lib to help you shorten and expand URLs using the most famous URL Shorteners available.

Installation

pip3 install pyshorteners

Code

TinyURL.com shortener implementation

import pyshorteners

url = input("Enter your url: ")
print("Your url: ", pyshorteners.Shortener().tinyurl.short(url))

#follow insta/@code_snail 

Output

image 2

Bit.ly shortener Implementation

Bitly: Bitly provides a platform to shorten URLs, share them, and keep a track of the activity on the shortened URL.

Before starting using Bitly API, you first need to signup on the site to get an API Key. This is very important to get access to the API to use it for programming.

To generate your OAuth access token:

  1. Click the profile menu.
  2. Click Profile Settings.
url shortener in python

3. Click Generic Access Token.

bit.ly url shortener in python

4. Enter your Bitly account password.

2020-05-04_09h15_23.png

5. Click Generate Token.

url shortener using python

6. Click Copy to copy the token. If you leave this setting and then access it again, you’ll be prompted to enter your password again to view the token.

url shortener in python

7. You can now paste the token into your code or settings to connect via the API.

Code

import pyshorteners

url = input("Enter your url: ")
s = pyshorteners.Shortener(api_key="YOUR_KEY")
print(s.bitly.short(url))

#follow insta/@code_snail

Output

url shortners in python

See codes for other url shortners services: https://pyshorteners.readthedocs.io/en/latest/apis.html#tinyurl-com

Hope you like this URL shortener code with python. Now make your own URL shortener in python. Must share it with your friends.

Other python mini projects,

Support CodeSnail

If you appreciate my work, or if it has helped you along your journey. It would mean a lot to me if you could write a message on my wall and share a cup of coffee (or tea) with me.

Buy Me A Coffee

Related Articles

1 thought on “URL Shortener in Python”

  1. Well written and done my friend!
    I started writing myself just recently and realised lot
    of people merely rehash old content but add very little
    of value. It’s fanttastic to read a useful article of some genuine value to myself and your other readers.

    It’s on mmy list of creteria I eed to replicate being a new blogger.
    Audience engagement and ccontent quality are king.
    Many excellent thoughts; you’ve unquestionably got on my list of people too follow!

    Carry on the fantastic work!
    Congratulations,
    Sydel

    Reply

Leave a Comment

Copy link
Powered by Social Snap