如何从任何字符串网址获取网站名称[关闭]

2023-11-30

我已经给出了包含任何有效 url 的字符串。 我必须从给定的网址中找到网站的名称。 我也忽略了子域。

like

http://www.yahoo.com   =>    yahoo
www.google.co.in =>      google
http://in.com    =>      in
http://india.gov.in/ => india
https://in.yahoo.com/ => yahoo
http://philotheoristic.tumblr.com/  =>tumblr
http://philotheoristic.tumblr.com/
https://in.movies.yahoo.com/        =>yahoo

这个怎么做


哟可以利用URL

来自文档 -http://docs.oracle.com/javase/tutorial/networking/urls/urlInfo.html

import java.net.*;
import java.io.*;

public class ParseURL {
    public static void main(String[] args) throws MalformedURLException {

        URL aURL = new URL("http://example.com:80/docs/books/tutorial"
                           + "/index.html?name=networking#DOWNLOADING");

        System.out.println("protocol = " + aURL.getProtocol());
        System.out.println("authority = " + aURL.getAuthority());
        System.out.println("host = " + aURL.getHost());
        System.out.println("port = " + aURL.getPort());
        System.out.println("path = " + aURL.getPath());
        System.out.println("query = " + aURL.getQuery());
        System.out.println("filename = " + aURL.getFile());
        System.out.println("ref = " + aURL.getRef());
    }
}

这是程序显示的输出:

protocol = http
authority = example.com:80
host = example.com                     // name of website
port = 80
path = /docs/books/tutorial/index.html
query = name=networking
filename = /docs/books/tutorial/index.html?name=networking
ref = DOWNLOADING

所以通过使用aURL.getHost()您可以获得网站名称。要忽略子域,您可以将其拆分为"."因此就变成了aURL.getHost().split(".")[0]只得到名字。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何从任何字符串网址获取网站名称[关闭] 的相关文章

随机推荐