Shakoor Ab
Shakoor Ab

Reputation: 53

Python URL Comparison

I have two URLS from which I have extracted hostnames using urlparse(). The result is: URL1='ads.indiaresults.com' and URL2='haryana.indiaresults.com'

Now how can I check if they are from the same domain or website. I have to make a general method so that it works on all URLS as some time hostnames are like: (www.google.com , www.e-tutes.com)

Upvotes: 0

Views: 2014

Answers (1)

Amperclock
Amperclock

Reputation: 409

This can be an answer:

Split your URLs :

URL1Split = URL1.split(".")
URL2Split = URL2.split(".")

Then, reverse the lists:

a = URL1Split[::-1]
b = URL2Split[::-1]

Now, you just have to pick the 2 firsts items to get the domain name:

domain1 = a[1] + "." + a[0]
domain2 = b[1] + "." + b[0]

Here's a function if you want :

def compDom(URL1,URL2):
  URL1Split = URL1.split(".")
  URL2Split = URL2.split(".")
  a = URL1Split[::-1]
  b = URL2Split[::-1]
  domain1 = a[1] + "." + a[0]
  domain2 = b[1] + "." + b[0]
  if domain1 == domain2:
      return 1
  else:
      return 0

Upvotes: 1

Related Questions