Extract links from a web page

前端 未结 6 1022
遇见更好的自我
遇见更好的自我 2020-12-01 08:22

Using Java, how can I extract all the links from a given web page?

6条回答
  •  醉话见心
    2020-12-01 08:54

    import java.io.*;
    import java.net.*;
    
    public class NameOfProgram {
        public static void main(String[] args) {
            URL url;
            InputStream is = null;
            BufferedReader br;
            String line;
    
            try {
                url = new URL("http://www.stackoverflow.com");
                is = url.openStream();  // throws an IOException
                br = new BufferedReader(new InputStreamReader(is));
    
                while ((line = br.readLine()) != null) {
                    if(line.contains("href="))
                        System.out.println(line.trim());
                }
            } catch (MalformedURLException mue) {
                 mue.printStackTrace();
            } catch (IOException ioe) {
                 ioe.printStackTrace();
            } finally {
                try {
                    if (is != null) is.close();
                } catch (IOException ioe) {
                    //exception
                }
            }
        }
    }
    

提交回复
热议问题