从网页获取所有链接

时间:2013-03-08 06:28:23

标签: java regex url hyperlink

我希望在执行GET后获取页面中的所有链接,我的代码适用于某些网站但不适用于其他网站,而调试时显示没有找到匹配项并且它永远不会进入while循环内部,尽管有链接在那个网站

  Pattern linkPattern = Pattern.compile("<a[^>]+href=[\"']?([\"'>]+)[\"']?[^>]*>(.+?)",               
    Pattern.CASE_INSENSITIVE | Pattern.DOTALL);
    Matcher pageMatcher = linkPattern.matcher(Content);

    if (FindKeyword(Content)) {
        LinksWithKey.add(HostName);
    }
        count++;

    while (pageMatcher.find()) {

1 个答案:

答案 0 :(得分:0)

与评论中所说的一样,您应该考虑使用JSoup来执行此类任务。

Document doc = Jsoup.parse(Content); // this is your original HTML content
for (Element link : doc.select("a[href]")) {
    System.out.println(link.attr("href"));
}
相关问题