所以我用Jsoup来抓取一些网页,这个有线问题就发生了。
// Sets the prefix for all pages to prevent navigate to unwanted pages.
String prefix = "https://handbook.unimelb.edu.au/%d/subjects";
// Postfix for search page
String searchPostfix = "(\\?page=\\d+)?$";
// Postfix for subject page
String subjectPostfix = "\\/(\\w+)(\\/.+)?$";
String root = String.format(prefix, "2019");
String pattern = root.replace("/", "\\/").replace(".", "\\.");
Pattern reg1 = Pattern.compile("^" + pattern + searchPostfix);
Pattern reg2 = Pattern.compile("^" + pattern + subjectPostfix);
使用这些正则表达式模式。我用绳子拉它
String s1 = "https://handbook.unimelb.edu.au/2019/subjects/undergraduate";
用一种方法:
private String getSubjectCode(String link) {
System.out.println(link);
if (isSubjectPage(link)) {
Matcher subjectMatcher = subjectPattern.matcher(link);
System.out.println(link);
// System.out.println(subjectMatcher.matches()); ## Exception if commented
System.out.println(subjectMatcher.group(0));
System.out.println(subjectMatcher.group(1));
return subjectMatcher.group(1);
}
return null;
}
但是,如果我评论这句话
Exception in thread "main" java.lang.IllegalStateException: No match found
at java.base/java.util.regex.Matcher.group(Matcher.java:645)
at Page.Pages.getSubjectCode(Pages.java:54)
at Page.Pages.enqueue(Pages.java:85)
at Crawler.Crawler.parsePage(Crawler.java:41)
at Crawler.Crawler.crawl(Crawler.java:51)
at Main.main(Main.java:9)
将引发上述异常,为什么打印行会影响程序的运行?
而且,没有任何评论
System.out.println(subjectMatcher.matches()); // Exception if commented
// out -> true