使用正則表達式及字符串操作,抽取網頁信息,實現代碼如下:
/* 去script */
public static String trimScript(String content) {
String regEx = "<script[^>]*>[^<]+</script>";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(content);
String result = content;
if (m.find()) {
result = m.replaceAll("");
}
return result;
}
/* 去除注釋*/
public static String trimComment(String content) {
String regEx = "<!--[^-]*-->";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(content);
String result = content;
if (m.find()) {
result = m.replaceAll("");
}
return result;
}
/* 去除標簽 */
public static String trimTag(String content) {
String regEx = "<[^>]+>";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(content);
String result = content;
if (m.find()) {
result = m.replaceAll("");
}
result = result.replace(" ", "").replace(">", "").replace(
">", "");
return result;
}
/* 根據起始位置和結束位置,截取字符串 */
public static String subString(String start, String end, String content) {
int iStart = content.indexOf(start);
int iEnd = content.indexOf(end);
if (iStart < iEnd) {
return content.substring(iStart, iEnd);
}
return null;
}