I have an url like: http://xxx.abcdef.com/fdfdf/
And I want to get xxx.abcdef.com
Which module can i use for accomplish this?
I want to use the same module and method at python2 and python3
I don't like the try except way for python2/3 compatibility
Thanks you so much!
You can use 3rd party library six, which takes care of compatibility issues between python versions and standard library function
urlparse
to extract the hostnameso all you need to do is install six and import urlparse
More on urlparse here
Use urlparse:
In Python 3, you import urlparse like so:
Alternatively, just use str.split():
Sidenote: Here's how you write an import of urlparse that will work in either version: