Find and display hyperlinks contained within a web page : HTML Parser « Network Protocol « Java

Find and display hyperlinks contained within a web page

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main {
  public static void main(String[] arguments) throws Exception{
    String page = loadPage(arguments[0]);
    Pattern pattern = Pattern.compile("<a.+href=\"(.+?)\"");
    Matcher matcher = pattern.matcher(page);
    while (matcher.find()) {

  static String loadPage(String name) throws Exception {
    StringBuffer output = new StringBuffer();
    FileReader file = new FileReader(name);
    BufferedReader buff = new BufferedReader(file);
    boolean eof = false;
    while (!eof) {
      String line = buff.readLine();
      if (line == null)
        eof = true;
        output.append(line + "\n");
    return output.toString();


Related examples in the same category

1.Escape HTML special characters from a String
2.Using javax.swing.text.html.HTMLEditorKit to parse html document
3.Extract links from an HTML page
4.extends HTMLEditorKit.ParserCallback
5.HTML parser based on HTMLEditorKit.ParserCallback
6.Get all hyper links from a web page
7.Getting the Links in an HTML Document
8.Getting the Text in an HTML Document
9.Use regular expression to get web page title