如何用Java实现提取网页中所有的超链接

2025-01-08 08:26:24
推荐回答(1个)
回答1:

import java.util.regex.*;

String baseURL = "http://...............";
Pattern pattern = Pattern.compile("href=\"(.*)\"",Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(webpage_content);
while(matcher.find()){
String foundURL = matcher.group(1);
if (foundURL.startsWith("http")){
System.out.println(foundURL);
}else{
System.out.println(baseURL + foundURL));
}
}