How to get host name from website using python

I would like to look get the host name using requests repository in python. I tried to do this like that:

pprint(requests.get("https://www.facebook.com/").headers['domain'])

but it doesn't work. If it is possible in other repository I would be grateful for any answer.

Upvotes: 0

Views: 248

Answers (1)

Mathieu Rollet
Mathieu Rollet

Reputation: 2364

Based on Regular Expression - Extract subdomain & domain

import requests
import re

p = re.compile("^(?:https?:\/\/)?(?:[^@\/\n]+@)?(?:www\.)?([^:\/?\n]+)")

r = requests.get("https://www.google.com/")
domain = p.match(r.url).group(1)
print(domain)

Upvotes: 1

Related Questions