解析一个html网页,读取指定的正文(去新闻广告)

来源:互联网 发布:python twisted 下载 编辑:程序博客网 时间:2024/05/16 11:05
//本例子是以以截取新浪新闻的正文为例子,其他的html类似
@Testpublic boolean getContent(String link) throws Exception {// 通过链接得到对应的输入流URL url = new URL(link);URLConnection urlConnection = url.openConnection();InputStream inputStream = urlConnection.getInputStream();// 把流存到一个byte[]数组contentByte中byte[] contentByte = new byte[1024 * 10];byte[] buffer = new byte[1024];int i = -1;while ((i = inputStream.read(buffer)) != -1) {contentByte = byteMerger(contentByte, buffer);}// 把数组转为字符串,再对字符串进行相应的增删改查.其中把byte[]数组转为String的时候要注意编码,要与link源的编码一致String str = new String(contentByte, "gb2312");// 下面是对字符串的截取int start = str.indexOf("<!-- 正文内容 begin -->");int end = str.indexOf("<!-- publish_helper_end -->");if (start != -1) {// 如果查找成功,即源文件存在要截取的目标// 截取需要的字符串String contentStr = str.substring(start, end);// 添加必要的html代码,主要转换的格式也要一一对应.添加的html的编码也要一致byte[] write1 = "<html xmlns=http://www.w3.org/1999/xhtml><head><meta http-equiv=Content-Type content=\"text/html;charset=GB2312\"><body>".getBytes("GB2312");byte[] write = contentStr.getBytes("GB2312");byte[] write2 = "</body></html>".getBytes();OutputStream outputStream = new FileOutputStream(Environment.getExternalStorageDirectory().toString() + "/tmp.html");// 用byteMerger方法来连接byte[]数组outputStream.write(byteMerger(byteMerger(write1, write), write2));outputStream.close();return true;} else {// 如果失败,就把源文件保存下来.查完出错原因FileWriter fw = new FileWriter(Environment.getExternalStorageDirectory().toString() + "/aa.txt");fw.flush();fw.write(str);fw.close();return false;}}// java 合并两个byte数组public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {byte[] byte_3 = new byte[byte_1.length + byte_2.length];System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);return byte_3;}

4 0
原创粉丝点击