1

How to display type of URL from the Given url as a string? example: String = http://introcs.cs.princeton.edu/python

and it should display edu.

8
  • Do you have a specific list of websites type? edu, and what else? Commented Nov 19, 2016 at 22:59
  • What if it ends in .co.uk? Commented Nov 19, 2016 at 23:00
  • it should display domain type. Commented Nov 19, 2016 at 23:04
  • another example: www.google.com Commented Nov 19, 2016 at 23:04
  • urllib.parse.urlparse('http://introcs.cs.princeton.edu/python').netloc.split('.') gives you ['introcs', 'cs', 'princeton', 'edu']. You can work it out from there. Commented Nov 19, 2016 at 23:04

1 Answer 1

0

I've written this code to get the domain type

from urlparse import urlparse
dom = ''
parsed_uri = urlparse( 'http://introcs.cs.princeton.edu/python' )
domain = '{uri.scheme}://{uri.netloc}'.format(uri=parsed_uri)
for i in range(len(domain.split('.'))):
    dom = domain.split('.')[i]

print dom

P.S. This code only works for URLs starting with a protocol, like http:// or https://

You can maybe get an idea from this. Cheers!

Sign up to request clarification or add additional context in comments.

4 Comments

This code worked too. Thanks!
I would have done if I had more than 15 reputations. xD
There you go. Good question though!
thanks and u got what you deserve! Thanks for the solution :)