从URL Python中提取域名 [英] Extract domain name from url Python
问题描述
我正试图从网址列表中提取域名.就像在
https://stackoverflow.com/questions/18331948/extract-domain-name-from-网址
我的问题是url可以包含所有内容,例如以下几个例子:
m.google.com
=> google
m.docs.google.com
=> google
www.someisotericdomain.innersite.mall.co.uk
=> mall
www.ouruniversity.department.mit.ac.us
=> mit
www.somestrangeurl.shops.relevantdomain.net
=> relevantdomain
www.example.info
=> example
等等..
域的多样性不允许我使用正则表达式,如 how从URL获取域名(因为我的脚本将在实时网络流量上运行,因此正则表达式必须足够大才能捕获如上所述的各种域).
不幸的是,我的网络研究并没有提供任何有效的解决方案.
有谁知道如何执行此操作?
任何帮助将不胜感激 !
谢谢
I am tring to extract the domain names out of a list of urls. just like in
https://stackoverflow.com/questions/18331948/extract-domain-name-from-the-url
My problem is that the urls can be about everything, few examples:
m.google.com
=> google
m.docs.google.com
=> google
www.someisotericdomain.innersite.mall.co.uk
=> mall
www.ouruniversity.department.mit.ac.us
=> mit
www.somestrangeurl.shops.relevantdomain.net
=> relevantdomain
www.example.info
=> example
And so on..
The diversity of the domains doesn't allow me to use a regex as shown in how to get domain name from URL (because my script will be running on real time network traffic, the regex will have to be enormous in order to catch all kinds of domains as mentioned).
Unfortunately my web research the didn't provide any efficient solution.
Does anyone have an idea of how to do this ?
Any help will be appreciated !
Thank you
推荐答案
使用tldextract
,这是urlparse
的更有效版本,tldextract
准确地分隔gTLD
或ccTLD
(通用或国家/地区代码顶部级别的域)来自已注册的URL的domain
和subdomains
.
Use tldextract
which is more efficient version of urlparse
, tldextract
accurately separates the gTLD
or ccTLD
(generic or country code top-level domain) from the registered domain
and subdomains
of a URL.
>>> import tldextract
>>> ext = tldextract.extract('http://forums.news.cnn.com/')
ExtractResult(subdomain='forums.news', domain='cnn', suffix='com')
>>> ext.domain
'cnn'
这篇关于从URL Python中提取域名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!