Html页面内容提取与过滤(很简单的例子、适合初学者)

第一种方案:简洁直观的逐个字符检查,去除html标签、空格、回车、换行、制表符等,代码如下:
package chapter2;
import java.io.*;
import java.net.*;


public class WebParser {


/**
* @param args
*/
private static String src_File_Path = "D:\\HtmlDownload\\htmlsrc.html";
private static String dst_File_Path = "D:\\HtmlDownload\\htmldst.txt";
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
try
{
Parser();
}
catch (IOException e)
{
System.err.println("下载失败,请检查地址是否正确!");
System.exit(1);
}
}

public static void Parser() throws IOException
{
try
{
boolean bContent = true;
StringBuffer sBuffer = new StringBuffer(8096*2);
char[] cBuffer = new char[8096*2];
int nCount = 0;
File srcfile = new File(src_File_Path);
FileReader fpReader = new FileReader(srcfile);
File dstfile = new File(dst_File_Path);
FileWriter fpWriter = new FileWriter(dstfile);
nCount = fpReader.read(cBuffer);
for(int i = 0; i < nCount; i++)
{
if(bContent==false)
{
if(cBuffer[i]=='>')
bContent = true;
else
continue;
}
else
{
if(cBuffer[i]=='<')
{
bContent = false;
continue;
}
else if(cBuffer[i]=='\n'||cBuffer[i]==' '||cBuffer[i]==' '||cBuffer[i]=='\t'||cBuffer[i]=='\r')
{
continue;
}
else if(cBuffer[i]=='&'&&cBuffer[i+1]=='n'&&cBuffer[i+2]=='b'&&cBuffer[i+3]=='s'&&cBuffer[i+4]=='p'&&cBuffer[i+5]==';')
{
i=i+5;
continue;
}
sBuffer.append(cBuffer[i]);
}
}
System.out.println(sBuffer.toString());
fpWriter.write(sBuffer.toString());
fpReader.close();
fpWriter.close();
}
catch (UnknownHostException e)
{
System.err.println("无法访问指定主机!");
System.exit(1);
}
catch (IOException e)
{
throw e;
}
}


}

第二种方案:采用正则表达式和替换的方式

package chapter2;
import java.io.*;
import java.net.*;


public class test {


/**
* @param args
*/
private static String src_File_Path = "D:\\HtmlDownload\\htmlsrc.html";
private static String dst_File_Path = "D:\\HtmlDownload\\testhtmldst.txt";
public static void main(String[] args) throws IOException
{
// TODO Auto-generated method stub
try
{
Parser();
}
catch (IOException e)
{
System.err.println("下载失败,请检查地址是否正确!");
System.exit(1);
}
}

public static void Parser() throws IOException
{
try
{
StringBuffer sBuffer = new StringBuffer(8096*2);

char[] cBuffer = new char[8096*2];
int nCount = 0;
File srcfile = new File(src_File_Path);
FileReader fpReader = new FileReader(srcfile);
File dstfile = new File(dst_File_Path);
FileWriter fpWriter = new FileWriter(dstfile);
nCount = fpReader.read(cBuffer);
for(int i = 0; i < nCount; i++)
{
sBuffer.append(cBuffer[i]);
}

String content=sBuffer.toString();
            content=content.replaceAll("</?[^>]+>","");   //剔出了的标签
            content=content.replace("&nbsp;","");
            content=content.replace(".","");
            content=content.replace("\"","‘");
            content=content.replace("'","‘");
            content=content.replaceAll("\\s*|\t|\r|\n","");//去除字符串中的空格,回车,换行符,制表符
            
System.out.println(content);
fpWriter.write(content);
fpReader.close();
fpWriter.close();
}
catch (UnknownHostException e)
{
System.err.println("无法访问指定主机!");
System.exit(1);
}
catch (IOException e)
{
throw e;
}
}


}


以上代码只是对html进行简单的处理,真正的网页分析要复杂得多。
本人只是初学搜索引擎,而且是一个没学过java的初学者,只会一些基本语法,完全不懂java web开发,错误和低劣的技术在所难免。
希望各位搜索引擎大牛或者java大牛多多指教。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值