0

I am modifying the code given in [crawler4j][1]. I want to find specific links while crawling a web site. For ex I am crawling on www.cmu.edu and I am trying to get the link for directory search. Here is my code for it -

public void visit(Page page) {          
    String url = page.getWebURL().getURL();
//  System.out.println("URL: " + url);

    if (page.getParseData() instanceof HtmlParseData) {
        HtmlParseData htmlParseData = (HtmlParseData) page.getParseData();
        String text = htmlParseData.getText();
        String html = htmlParseData.getHtml();
        System.out.println(html.matches(".*<a href.*."));
        if (html.matches(".*.<a href=.*.>Directory Search</a>.*."))
            System.out.println("***********Hello*********************");
        //  System.out.println("----------"+html);
        return;
//      List<WebURL> links = htmlParseData.getOutgoingUrls();
    }
}

This code does not work. I am not getting the *******Helo********* on my console. Just to check I printed the html string in console and I copied the anchor tag that contains the directory sreach and I wrote this simple two line code -

String test2="<li class=\"first\"><a href=\"http://directory.andrew.cmu.edu/\" title=\"Carnegie Mellon University Faculty, Staff and Student Directory\">Directory Search</a></li>";
System.out.println("*******"+test2.matches(".*.<a href=.*.>Directory Search</a>.*."));

This works. The value of String test2 is copied from the console. What am I doing wrong in the first part of the code?

[1]

RevanProdigalKnight
  • 1,316
  • 1
  • 14
  • 23

1 Answers1

0

Try this (you have to use (?s) to match also new line characters)

String test2="qwert\n\n<li class=\"first\"><a href=\"http://directory.andrew.cmu.edu/\" title=\"Carnegie Mellon University Faculty, Staff and Student Directory\">Directory Search</a></li>";
System.out.println("*******"+test2.matches("(?s).*.<a href=.*.>Directory Search</a>.*."));
slavik
  • 1,223
  • 15
  • 17