从MongoDB集合的URL中获取域名。

问题描述 投票:1回答:1

我正试图从一个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 为其他一切。

mongodb mongodb-query nosql substring pymongo
1个回答
1
投票

使用 urlparse 来自 urllib.解析库.

from urllib.parse import urlparse
url = urlparse('https://google.bing.com/Jumbo-Privacy')
print (url.netloc)

给予。

google.bing.com

0
投票

你必须写一些自定义代码来解析它。

下面是一个例子。


//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]


© www.soinside.com 2019 - 2024. All rights reserved.