如何获取一个url主域名
目录
前言
今天公司给了我一个任务让我获取一批图片的主域名,给到我任务的时候我想这也太简单了,一会写完又可以滑水了。
然后架构过来给我说:好好想想怎么拿到主域名。
我说:这不简单,字符串截取不就拿到了,然后架构说了一句哪有那么简单。
我:??????
提示:以下是本篇文章正文内容,下面案例可供参考
一、主域名是什么?
主域名一般是开通主机时候绑定的域名,一般是绑定在网站的根目录,一级域名其实和主域名是2个不同的概念,主域名可以是二级域名或者是三级域名。
首先我们来先看一批url:
www.google.com 主域名:google.com
ww.socialrating.it 主域名:socialrating.it
www-01.hopperspot.com 主域名:hopperspot.com
wwwsupernatural-brasil.blogspot.com 主域名:blogspot.com
www.people.com.cn 主域名:people.com.cn
mp.weixin.qq.com 主域名:qq.com
从上面这些域名中可以看出,有的是二级域名是主域名,有的是三级域名是主域名,没办法做判断
二、获取主域名的几种方式
一、使用正则表达式
((?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)),这个我这里就不多说了,百度一下还是可以搜出来的,这种方式获取国内的是没有问题的,但是如果涉及到国外,你不可能把每个国家的主域名都差个遍放到正则表达式中把
String url = "https://m.weibo.cn/status/4126638973026114";
Pattern pattern = Pattern.compile("(?<=http://|\\.)[^.]*?\\.(com|cn|net|org|biz|info|cc|tv)",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(url);
matcher.find();
System.out.println(matcher.group());
二、使用阿里的API(GetMainDomainName)
1、首先导入依赖
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-alidns</artifactId>
<version>2.6.29</version>
</dependency>
<!-- 这个依赖好像也有引入,看自己情况,我当时好像只引入第一个不行,如果这个不行就引入阿里OSS依赖 -->
<dependency>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.3.7</version>
</dependency>
2、使用
@Test
public void aliGetDomainName() throws MalformedURLException, ClientException {
String redionld = "cn-shanghai";
//你自己的阿里云accessKeyId和accessKeySecret
String accessKeyId = "LTAI5tLjbEcm我这里没写全你写你自己的";
String accessKeySecret = "3L7rLvkcw7IRDf我这里没写全你写你自己的";
//定义url
String urlStr = "http://mp.weixin.qq.com/s/4126638973026114";
// String urlStr = "http://www.people.com.cn/";
//首先先拿到域名
//因为阿里的API只能读取域名,不能读取整个url
URL url = new URL(urlStr);
//通过getHost方法可以仅获取的域名,类似于这样的:www.people.com.cn
String host = url.getHost();
//通过阿里api获取主域名
//设置阿里的一些key
DefaultProfile profile = DefaultProfile.getProfile(redionld, accessKeyId, accessKeySecret);
IAcsClient client = new DefaultAcsClient(profile);
GetMainDomainNameRequest request = new GetMainDomainNameRequest();
request.setInputString(host);
GetMainDomainNameResponse response = client.getAcsResponse(request);
//主域名
String domainName = response.getDomainName();
System.err.println(domainName);
}
结果
缺陷:因为我当时要跑的url比较多,准备用多线程跑,然后发现阿里云的这个不能支持高并发,单线程数据少量数据还是足够用的,然后我就去问了阿里的人,他们的人对我说这个接口是对内访问的,每秒访问频率100次,但是我自己测下来根本没有100,因为我们调他们的接口是走网络的,所以也没办法解决。
三、Google永远的神
谷歌的guava里面有一个工具类,InternetDomainName,在这个包下import com.google.common.net.InternetDomainName
首先引入依赖
<!-- google获取主域名-->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>28.0-jre</version>
</dependency>
/**
* 获取主域名方法
* guava 获取主域名
*
*
* @return
*/
@Test
public void googleGetDomainName() throws MalformedURLException {
String urlStr = "http://www.people.com.cn/";
//首先对url进行处理,from中要求的格式必须为:mp.weixin.qq.com,不能有http://开头,不能有/结尾
URL url = new URL(urlStr);
String host = url.getHost();
//使用谷歌的工具类获取主域名,他返回的不是一个string类型的
InternetDomainName internetDomainName = InternetDomainName.from(host).topPrivateDomain();
//将主域名变成string类型
String domainName = internetDomainName.toString();
System.err.println("domainName = " + domainName);
}
结果
总结
记录我的第一篇博客,还是有很多的不懂,以后会慢慢学习
上面这些代码都可以提取为通用方法
如果哪里有错欢迎各位大佬评论区指正,在这就提前谢谢各位大佬了
---------------------------------------------------------------------------------------------------------------------------------
更新补充:上面 InternetDomainName internetDomainName = InternetDomainName.from(host).topPrivateDomain();这一行代码有点问题在我实际的生产环境中
问题:
String str = "https://inspirationsdeco.blogspot.com";这个url的域名出来还是inspirationsdeco.blogspot.com
解决:使用另外一个方法topDomainUnderRegistrySuffix()
InternetDomainName internetDomainName = InternetDomainName.from(urlHost).topDomainUnderRegistrySuffix();