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.

alan
  • 391
  • 1
  • 4
  • 8

2 Answers2

6

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"));
}
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
4

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();
Jonathan Holloway
  • 62,090
  • 32
  • 125
  • 150