• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Using Python to Get Domain from URL

Using Python to Get Domain from URL

May 8, 2022 Leave a Comment

To get the domain from a URL in Python, the easiest way is to use the urllib.parse module urlparse() function and access the netloc attribute.

from urlparse.parse import urlparse

domain = urlparse("http://theprogrammingexpert.com/python-get-domain-from-url").netloc

print(domain)

#Output:
theprogrammingexpert.com

When working with URLs in Python, the ability to easily extract information about those URLs can be very valuable.

One such piece of information which is valuable to have given a URL is the domain name.

We can use Python to easily get the domain of a URL using the urllib.parse module.

The urllib.parse module has the function urlparse() which parses a URL and returns an object with pieces of information about the URL such as scheme, domain, path, the query string, etc.

With urlparse(), you can get the domain from a URL. The domain will be stored in the return value’s ‘netloc’ attribute.

Below is a simple example of how you can get the domain from a URL using Python.

from urlparse.parse import urlparse

domain = urlparse("http://theprogrammingexpert.com/python-get-domain-from-url").netloc

print(domain)

#Output:
theprogrammingexpert.com

Using urlparse() Function to Get Other Pieces of Information about URLs in Python

The urlparse() function allows you to get other pieces of information of a URL. When you use urlparse(), you get back a 6-tuple which has information such as scheme, domain, path, the query string, etc.

Below is an example showing the information you will get back if you use urlparse() in your Python code.

from urlparse.parse import urlparse

print(urlparse("http://theprogrammingexpert.com/python-get-domain-from-url/"))

#Output:
ParseResult(scheme='https',netloc='theprogrammingexpert.com', path='/python-get-domain-from-url/", params='', query='', fragment=''

Hopefully this article has been useful for you to learn how to get the domain name from a URL with Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Read File Character by Character
  • 2.  How to Write CSV File to AWS S3 Bucket Using Python
  • 3.  Convert String to Integer with int() in Python
  • 4.  How to Check if List is Empty in Python
  • 5.  Truncate String in Python with String Slicing
  • 6.  How to Rotate a List in Python
  • 7.  Golden Ratio Constant phi in Python
  • 8.  Transpose DataFrame pandas – Using the pandas transpose Function
  • 9.  Drop Duplicates pandas – Remove Duplicate Rows in DataFrame
  • 10.  Using Python to Remove Apostrophe From String

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy