Home > Software design >  Parse part of url- python
Parse part of url- python

Time:11-09

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:]

CodePudding user response:

There is standard module for URL parsing

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

CodePudding user response:

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='')
  • Related