tags:

views:

91

answers:

2

Using Java have the source code of a webpage stored in a string. I want to extract all the urls in the source code and output them. I am awful with regex and the like and have no idea how to even approach this. Any help would be greatly appreciated.

+4  A: 

Don't use regex. Use a parser like JSoup.

String html = "your html string";
Document document = Jsoup.parse(html); // Can also take an URL.
for (Element element : document.getElementsByTag("a")) {
    System.out.println(element.attr("href"));
}
BalusC
+3  A: 

You could use HtmlUnit, then to extract the links it's as simple as:

WebClient wc = new WebClient();
URL url = new URL("http://www.oogly.co.uk/");
HtmlPage page = (HtmlPage) wc.getPage(url);
PrintWriter printWriter = new PrintWriter(new FileWriter(FILE_NAME));
List anchors = page.getAnchors();
Jon