我试图从URL列表中提取域名。就像在
https://stackoverflow.com/questions/18331948/extract-domain-name-from-the-url
我的问题是,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 to get domain name from URL中所示的正则表达式(因为我的脚本将运行在来自真实网络流量的大量URL上,所以正则表达式必须非常庞大才能捕获所提到的所有类型的域)。
不幸的是,我的网络研究没有提供任何有效的解决方案。
有人知道怎么做吗?
任何帮助都将不胜感激
多谢各位
似乎可以对该url使用urlparsehttps://docs.python.org/3/library/urllib.parse.html,然后提取netloc
从netloc中,您可以使用split轻松提取域名
通过正则表达式的简单解决方案
使用
tldextract
这是urlparse
的更有效版本,tldextract
准确地将gTLD
或ccTLD
(通用或国家代码顶级域)从URL的注册domain
和subdomains
中分离出来相关问题 更多 >
编程相关推荐