从python中的URL中提取域 [英] Extract domain from URL in python

查看:30
本文介绍了从python中的URL中提取域的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的网址:
http://abc.hostname.com/somethings/anything/

I have an url like:
http://abc.hostname.com/somethings/anything/

我想得到:
hostname.com

我可以使用什么模块来完成此任务?
我想在python2中使用相同的模块和方法.

What module can I use to accomplish this?
I want to use the same module and method in python2.

推荐答案

对于在 Python 3 中解析 URL 的域,您可以使用:

For parsing the domain of a URL in Python 3, you can use:

from urllib.parse import urlparse

domain = urlparse('http://www.example.test/foo/bar').netloc
print(domain) # --> www.example.test

然而,为了可靠地解析顶级域(在本例中为example.test),您需要安装一个专门的库(例如,tldextract).

However, for reliably parsing the top-level domain (example.test in this example), you need to install a specialized library (e.g., tldextract).

这篇关于从python中的URL中提取域的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆