MrmDdda
MrmDdda

Reputation: 51

Parse part of url- python

I have two types of addresses in DataBase:

url_1  = "http://country.city.street/"
url_2  = "http://country.city.street:8180/"

I need to get a uniform address format (url_pattern = "country.city.street") to use in a DNS server. I removed the http:// part from the beginning but can't get a good result with the end of the address. Does anyone have an idea what I could use to get a url_pattern standard?

url_1  = "http://country.city.street/"
url_2  = "http://country.city.street:8180/"

url_1 = url_1[7:]
url_2 = url_2[7:]

Upvotes: 0

Views: 285

Answers (2)

user459872
user459872

Reputation: 24602

You can use urllib.parse module. It has a urlparse function that you could use to parse a URL into components.

>>> import urllib.parse
>>> urllib.parse.urlparse("http://country.city.street/")
ParseResult(scheme='http', netloc='country.city.street', path='/', params='', query='', fragment='')

Upvotes: 2

farincz
farincz

Reputation: 5173

There is standard module for URL parsing

from urllib.parse import urlparse
print(urlparse("http://country.city.street:8180/").hostname)

Upvotes: 2

Related Questions