我正试图从一个URL字段中获取域名。目前,它作为一个URL存储在集合中。https://google.bing.com/Jumbo-Privacy
. 我只需要 google.bing.com
.
对于URL为 www.google.com
我只是想 google.com
. 有什么办法可以让我在显示集合结果的时候直接做到这一点吗?
当我添加 'domain': {'$arrayElemAt': [ { '$split': ["$url", "/"] }, 2 ] },
它的工作和回报 google.bing.com
对于 https://google.bing.com/Jumbo-Privacy
. 但仍然返回 www
为其他一切。
使用 urlparse
来自 urllib.解析库.
from urllib.parse import urlparse
url = urlparse('https://google.bing.com/Jumbo-Privacy')
print (url.netloc)
给予。
google.bing.com
你必须写一些自定义代码来解析它。
下面是一个例子。
//Assuming you'll always have https:// infront
const Url = 'https://moodli.org/geo'
//Split thé URL into an array using the forward slash
let urlSplit = Url.split('/');
//Get the domain name
let domian = urlSplit[2]