sankar nataraj
sankar nataraj

Reputation: 83

how to capture the current domain name in a URL using flask python

I want to capture a domain name dynamically from any websites using python.

 Example: https://stackoverflow.com/questions/ask
expected output: stackoverflow.com

Here everything should be dynamic, Dynamic URL, Dynamic Domain Name.

Upvotes: 0

Views: 758

Answers (1)

chluebi
chluebi

Reputation: 1829

I reccomend using urllib.parse.

from urllib.parse import urlparse
o = urlparse('https://stackoverflow.com/questions/ask')
base_url = f'{o.scheme}://{o.hostname}'
# or just the hostname, which is what you probably want
hostname = o.hostname

Edit: Just in case I'm misunderstanding you, here's a function that would return the hostname for every url you enter:

from urllib.parse import urlparse

def get_hostname(url):
    return urlparse.host(url)

Upvotes: 1

Related Questions