首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从MongoDB集合中的URL获取域名

问从MongoDB集合中的URL获取域名
EN

Stack Overflow用户
提问于 2020-04-23 07:10:25
回答 2查看 198关注 0票数 0

我正在尝试从URL字段中获取域名。目前,它以URL:https://google.bing.com/Jumbo-Privacy的形式存储在集合中。我只需要google.bing.com。

对于URL as www.google.com,我只需要google.com。当我显示收集结果时,有没有什么方法可以让我直接这样做呢?

WHen I添加'domain': {'$arrayElemAt': [ { '$split': ["$url", "/"] }, 2 ] },

它可以工作,并为https://google.bing.com/Jumbo-Privacy返回google.bing.com。但仍然返回其他所有内容的www。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-04-23 22:23:45

从urllib.parse library使用urlparse。

代码语言:javascript
复制
from urllib.parse import urlparse
url = urlparse('https://google.bing.com/Jumbo-Privacy')
print (url.netloc)

提供:

代码语言:javascript
复制
google.bing.com
票数 1
EN

Stack Overflow用户

发布于 2020-04-23 07:26:48

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

下面是一个示例:

代码语言:javascript
复制
//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]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61376633

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档