Java实现抽取网页信息
来源:优易学  2011-11-29 14:18:42   【优易学:中国教育考试门户网】   资料下载   IT书店
  使用正则表达式及字符串操作,抽取网页信息,实现代码如下:/* 去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;
  }

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训