Finding Links on a Webpage with Java

徘徊边缘 提交于 2020-01-24 12:48:08

问题


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.


回答1:


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"));
}



回答2:


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();


来源:https://stackoverflow.com/questions/2717760/finding-links-on-a-webpage-with-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!