Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8028237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T00:04:45+00:00 2026-06-05T00:04:45+00:00

I am trying to extract the URL from the given String , which contain

  • 0

I am trying to extract the URL from the given String, which contain the HTTP response with HREF tag. I have reached the beginning of the links but I need to terminate the string as soon as the HREF ends. How this could be achieved?

public class Extracturl {
public static void main(String[] args) throws IOException {
    // TODO Auto-generated method stub
    String line;

    try {
        String u="http://en.wikipedia.org/wiki/china";
        String fileName = "e:\\test.txt";
         BufferedWriter writer = new BufferedWriter(new FileWriter(fileName,true));
        url = new URL(u);
        is = url.openStream();  // throws an IOException
        dis = new DataInputStream(new BufferedInputStream(is));

        String w=new String();
        while ((line = dis.readLine()) != null) {


                try {
   if(line.contains("href=\"/wiki")&&line.contains("\" />")&& (!line.contains("File")))
                    {   

                    if(!w.contains(line.substring(line.indexOf("href=\"/"))))
                    {w=w+line.substring(line.indexOf("href=\"/"));                        
                        System.out.println(line.substring(line.indexOf("href=\"/"))); 
                    writer.write(w);
                    writer.newLine();
                    }}
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }
    } catch (MalformedURLException mue) {
         mue.printStackTrace();
    } catch (IOException ioe) {
         ioe.printStackTrace();
    } finally {
        try {
            is.close();

           // writer.close();
        } catch (IOException ioe) {
            // nothing to see here
        }
    }
}

    }

I even tried

   w=w+line.substring(line.indexOf("href=\"/"),line.indexOf("\">"));

But this gave me error.

My aim is to get all the URLs which are linked from the page.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T00:04:46+00:00Added an answer on June 5, 2026 at 12:04 am

    Use an HTML parser for that purpose. Here is an example with the embedded Java HTML parser. There are other alternatives like JSoup, but for basic HTML handling, this one does a pretty good job:

    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.net.MalformedURLException;
    import java.net.URL;
    import java.util.LinkedHashSet;
    import java.util.Set;
    
    import javax.swing.text.MutableAttributeSet;
    import javax.swing.text.html.HTML;
    import javax.swing.text.html.HTML.Tag;
    import javax.swing.text.html.HTMLEditorKit;
    import javax.swing.text.html.parser.ParserDelegator;
    
    public class URLExtractor {
    
        private static class HTMLPaserCallBack extends HTMLEditorKit.ParserCallback {
    
            private Set<String> urls;
    
            public HTMLPaserCallBack() {
                urls = new LinkedHashSet<String>();
            }
    
            public Set<String> getUrls() {
                return urls;
            }
    
            @Override
            public void handleSimpleTag(Tag t, MutableAttributeSet a, int pos) {
                handleTag(t, a, pos);
            }
    
            @Override
            public void handleStartTag(Tag t, MutableAttributeSet a, int pos) {
                handleTag(t, a, pos);
            }
    
            private void handleTag(Tag t, MutableAttributeSet a, int pos) {
                if (t == Tag.A) {
                    Object href = a.getAttribute(HTML.Attribute.HREF);
                    if (href != null) {
                        String url = href.toString();
                        if (!urls.contains(url)) {
                            urls.add(url);
                        }
                    }
                }
            }
        }
    
        public static void main(String[] args) throws IOException {
            InputStream is = null;
            try {
                String u = "http://en.wikipedia.org/wiki/china";
                URL url = new URL(u);
                is = url.openStream(); // throws an IOException
                HTMLPaserCallBack cb = new HTMLPaserCallBack();
                new ParserDelegator().parse(new BufferedReader(new InputStreamReader(is)), cb, true);
                for (String aUrl : cb.getUrls()) {
                    System.out.println("Found URL: " + aUrl);
                }
            } catch (MalformedURLException mue) {
                mue.printStackTrace();
            } catch (IOException ioe) {
                ioe.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException ioe) {
                    // nothing to see here
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to extract a page name and query string from a URL which
I'm trying to extract an anchor tag and everything behind it from a url
I am trying to extract href and src links from an HTML string. According
I'm trying to find a reliable solution to extract a url from a string
I'm trying to programmatically extract some data from this url: http://www.treasurydirect.gov/NP/BPDLogin?application=np The issue is
Extract text from URL ? trying this preg_match /\<a href=([^]*) .?\>([^\<\/a]*)\<\/a\>+/ Not working on
I am trying to extract the video id from a youtube url using the
I am trying to extract the page name from a complex url. This post
I am trying to extract the GET parameters from a ZF REST URL. It's
I have a url and I am trying to extract the text before the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.