【问题标题】:python 2 and 3 extract domain from urlpython 2和3从url中提取域
【发布时间】:2014-02-04 21:23:32
【问题描述】:
我有一个类似的网址:http://xxx.abcdef.com/fdfdf/
我想得到xxx.abcdef.com
我可以使用哪个模块来完成这个?
我想在 python2 和 python3 使用相同的模块和方法
我不喜欢 python2/3 兼容性之外的尝试方式
非常感谢!
【问题讨论】:
标签:
python
parsing
python-3.x
compatibility
python-2.x
【解决方案1】:
使用urlparse:
from urlparse import urlparse
o = urlparse("http://xxx.abcdef.com/fdfdf/")
print o
print o.netloc
在 Python 3 中,您可以像这样导入 urlparse:
from urllib.parse import urlparse
或者,只需使用str.split():
url = "http://xxx.abcdef.com/fdfdf/"
print url.split('/')[2]
旁注:以下是您编写可在任一版本中使用的 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
【解决方案2】:
您可以使用第三方库六,它负责处理python版本和标准库函数urlparse之间的兼容性问题来提取主机名
所以您需要做的就是install six 并导入 urlparse
from six.moves.urllib.parse import urlparse
u = urlparse("http://xxx.abcdef.com/fdfdf/")
print(u.hostname)
更多关于 urlparse here