Prashant Prashant - 2 years ago 109
Python Question

How to get the home page URL link

Let's say I am on the webpage

How could I get the URL of home page of the company/website in Java/Python which is

(in this case)

Now this seems so easy for some cases, but I want to generalise this & unable to cover all cases like that of slack/google_design/etc....

Say similar cases are:

Expected Output:

Expected Output:

Expected Output:

The code from the link below:

#include <iostream>
#include <string>

using namespace std;

int main() {
string s = "";
int cnt=0;
int p;
int l=s.length();
for(int i=0;i<l;i++)
if(s[i]=='/' && cnt!=3)
if(s[i]=='/' && cnt==3){
return 0;

Please see JonasCz's 2nd comment on his own answer that actually helped

Answer Source

You can use something like this:

URL aURL = new URL("");
System.out.println(aURL.getProtocol() + "://" + aURL.getHost());

Which prints:

This works for other URLs too. See the docs for more details.

If you want to get only the main domain, without the subdomain (i.e. only, you can use Guava's InternetDomainName, eg. like this:


The above will return

To be complete, the whole code, in your case, would look like this:

URL aURL = new URL("");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download