10 examples of 'python get domain from url' in Python

Every line of 'python get domain from url' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
43def get_domain_from_url(path):
44 try:
45 domain, = domain_url_re.search(path).groups()
46 except Exception:
47 domain = None
48 return domain
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

90def getDomainFromURL(url):
91 # QUESTION-TODO added .split('\\')[0], because sometime URL looked like: http://domain\
92 domain = url.split('://')[1].split('/')[0].split(':')[0].split('\\')[0]
93 if(domain[-1] == '.'):
94 domain = domain[:-1]
95 return domain
6def domain_for_url(url):
7 # Strip out everything from url but the hostname
8 hostname = url.strip()
9 hostname = hostname.replace("http://", "")
10 hostname = hostname.replace("https://", "")
11 hostname = DomainIsolator.match(hostname).groups()[0]
12 hostname = hostname.split(".")
13
14 # We will still want to throw away most of the hostname
15 if len(hostname) >= 2:
16 # Take the top-level and second-level domain
17 domain = hostname[len(hostname) - 2] + '.' + hostname[len(hostname) - 1]
18 # If this is one of the known second-level domains, take the third
19 # level as well
20 if domain in DomainList and len(hostname) >= 3:
21 domain = hostname[len(hostname) - 3] + '.' + domain
22 else:
23 domain = '.'.join(hostname)
24
25 return domain
118def get_domain(self, full_url):
119 """function to extract the domain name from the URL"""
120 clean_reg= re.compile(r'^((?:https?:\/\/)?(?:www\.)?).*?(\/.*)?$')
121 match = re.search(clean_reg, full_url)
122 beg, end = match.group(1), match.group(2)
123 domain = string.replace(full_url, beg, '')
124 domain = string.replace(domain, end, '')
125 return domain
535def url_host_domain(url):
536 """
537 Return a tuple of the (host, domain) of a URL or None. Assumes that the
538 URL has a scheme.
539 """
540 try:
541 parsed = urlpy.parse(url)
542 host = parsed.host if py3 else parsed._host
543 if not host:
544 return None, None
545 domain = parsed.pld if py3 else parsed.pld()
546 return host.lower(), domain.lower()
547 except Exception as e:
548 if TRACE:
549 logger_debug('url_host_domain: failed for:', url, 'with:', repr(e))
550 # ignore it
551 return None, None
3def get_domain(s):
4 if '@' in s:
5 # it's an email address
6 domain = s[s.index('@') + 1:]
7 else:
8 result = urlparse.urlparse(s)
9 domain = result.netloc
10 return domain
148def get_domain(self, url=""):
149 domain = url[0].replace("http://", '')
150 domain = domain.replace("https://", '')
151 domain = domain.split("/")
152 return domain[0]
77def getHost(url) :
78 exp = re.compile("^(https?\:\/\/)((([\da-z\.-]+)\.([a-z\.]{2,6}))|[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3})(:[0-9]+)?([\/\w \.-]*)\/?([\/\w \.-]*)\/?((\?|&).+?(=.+?)?)*$")
79 res = exp.match(url)
80 return str(res.group(2))
282def get_host(url):
283 """
284 Given a url, return its scheme, host and port (None if it's not there).
285
286 For example:
287 >>> get_host('http://google.com/mail/')
288 http, google.com, None
289 >>> get_host('google.com:80')
290 http, google.com, 80
291 """
292 # This code is actually similar to urlparse.urlsplit, but much
293 # simplified for our needs.
294 port = None
295 scheme = 'http'
296 if '//' in url:
297 scheme, url = url.split('://', 1)
298 if '/' in url:
299 url, path = url.split('/', 1)
300 if ':' in url:
301 url, port = url.split(':', 1)
302 port = int(port)
303 return scheme, url, port
10def __get_domain(uri):
11 url = urlparse(uri)
12 return '{}://{}'.format(url.scheme, url.netloc)

Related snippets