简体   繁体   English

python 2和3从url提取域

[英]python 2 and 3 extract domain from url

I have an url like: http://xxx.abcdef.com/fdfdf/ 我有一个网址,例如: http://xxx.abcdef.com/fdfdf/ : http://xxx.abcdef.com/fdfdf/

And I want to get xxx.abcdef.com 我想得到xxx.abcdef.com

Which module can i use for accomplish this? 我可以使用哪个模块来完成此任务?

I want to use the same module and method at python2 and python3 我想在python2和python3使用相同的模块和方法

I don't like the try except way for python2/3 compatibility 我不喜欢尝试python2 / 3兼容性的方法

Thanks you so much! 非常感谢!

Use urlparse : 使用urlparse

from urlparse import urlparse
o = urlparse("http://xxx.abcdef.com/fdfdf/")
print o

print o.netloc

In Python 3, you import urlparse like so: 在Python 3中,您可以这样导入urlparse

from urllib.parse import urlparse

Alternatively, just use str.split() : 或者,只需使用str.split()

url = "http://xxx.abcdef.com/fdfdf/"

print url.split('/')[2]

Sidenote: Here's how you write an import of urlparse that will work in either version: 旁注:这是您编写可在任一版本中使用的urlparse导入的方法:

if sys.version_info >= (3, 0):
    from urllib.parse import urlparse
if sys.version_info < (3, 0) and sys.version_info >= (2, 5):
    from urlparse import urlparse

You can use 3rd party library six, which takes care of compatibility issues between python versions and standard library function urlparse to extract the hostname 您可以使用第三方库六,它可以处理python版本与标准库函数urlparse之间的兼容性问题,以提取主机名

so all you need to do is install six and import urlparse 所以您要做的就是安装六个并导入urlparse

from six.moves.urllib.parse import urlparse
u = urlparse("http://xxx.abcdef.com/fdfdf/")
print(u.hostname)

More on urlparse here 有关urlparse的更多信息,请点击此处

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM