extract domain name from url
Sometimes I need to find domain name from url in my program for various purposes (most of the time in my crawlers). So far I used the following function that takes an url and returns the domain name:
But today I found a module named urlparse. So my function now looks like this:
The new one is much better I think.
Check urlparse for details.
def find_domain(url):
pos = url[7:].find('/')
if pos == -1:
pos = url[7:].find('?')
if pos == -1:
return url[7:]
url = url[7:(7+pos)]
return url
But today I found a module named urlparse. So my function now looks like this:
def find_domain2(url):
return urlparse(url)[1]
The new one is much better I think.
Check urlparse for details.
Comments
from urlparse import urlparse
parsed = urlparse('http://example.com')
print parsed.hostname