在 Python 中从 URL 中提取域名 [英] Extract domain name from URL in Python

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

问题描述

我想从 URL 列表中提取域名.就像在https://stackoverflow.com/questions/18331948/extract-domain-name-from-网址
我的问题是 URL 可以包含所有内容,仅举几个例子:
m.google.com =>谷歌
m.docs.google.com =>谷歌
www.someisotericdomain.innersite.mall.co.uk =>商城
www.ouruniversity.department.mit.ac.us =>提交
www.somestrangeurl.shops.relevantdomain.net =>相关域
www.example.info =><代码>示例
等等..
域的多样性不允许我使用 how 中所示的正则表达式从 URL 获取域名(因为我的脚本将在来自真实网络流量的大量 URL 上运行,正则表达式必须非常庞大才能捕获上述的所有类型的域).
不幸的是,我的网络研究没有提供任何有效的解决方案.
有没有人知道如何做到这一点?
任何帮助将不胜感激 !
谢谢

解决方案

使用tldextracturlparse的更高效版本,tldextract准确将 gTLDccTLD(通用或国家代码顶级域)与注册的 domainsubdomains 分开一个网址.

<预><代码>>>>进口文摘>>>ext = tldextract.extract('http://forums.news.cnn.com/')ExtractResult(subdomain='forums.news', domain='cnn', suffix='com')>>>外域'cnn'

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 enormous amount of urls from real 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

解决方案

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'

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

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