我正在尝试从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。
发布于 2020-04-23 22:23:45
从urllib.parse library使用urlparse。
from urllib.parse import urlparse
url = urlparse('https://google.bing.com/Jumbo-Privacy')
print (url.netloc)提供:
google.bing.com发布于 2020-04-23 07:26:48
您必须编写一些自定义代码来解析它。
下面是一个示例:
//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]https://stackoverflow.com/questions/61376633
复制相似问题