Create a URL shortener using Python code

  • Post author:
  • Post comments:0 Comments
  • Reading time:21 mins read

Unlock the world of efficient and convenient URL management with Python as we dive into building a URL shortener. In this project, you’ll learn how to create a Python script that transforms long, unwieldy URLs into concise, easy-to-share links.

Whether you’re a web developer, a marketer, or simply someone looking to simplify and track your URL usage, this step-by-step guide will help you create your own URL shortener. Join us as we explore the power of Python in making the internet a more user-friendly place, one link at a time!


import requests
import json

UI = input("Enter the long link: ")
api_key = 'You api key here'
headers = {
    'Authorization': f'Bearer {api_key}',
    'Content-Type': 'application/json',
}

data = {"long_url": UI}
for i in range(3): 
    result = requests.post("https://api-ssl.bitly.com/v4/shorten", headers=headers, data=json.dumps(data))
    if result.status_code == 200:
        break
if result.status_code == 200:
    link = json.loads(result.content)['link']
    print(f"\nYour shortened link: {link}")
else:
    print("error occured")
Publisher
Latest posts by Publisher (see all)

Publisher

Publisher @ideasorblogs

Leave a Reply