• 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
  • VBA
  • 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.  How to Save and Use Styles from Existing Word File With python-docx
  • 2.  Using Python turtle Module to Draw Square
  • 3.  Using Python Selenium Webdriver to Refresh Page
  • 4.  Print Approximately Equal Symbol in Python
  • 5.  Print Object Attributes in Python using dir() Function
  • 6.  How to Multiply All Elements in List Using Python
  • 7.  How to Check if Number is Negative in Python
  • 8.  Are Tuples Mutable in Python? No, Tuples are not Mutable
  • 9.  Selenium maximize_window() Function to Maximize Window in Python
  • 10.  Get Last Day of Month Using Python

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy