|
|
浅析JAVA实现网页取内容 |
|
|
作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站 |
有很多网站提供从其他网站提取新闻甚至是从向翻译网站取内容 由于手头做的网站需要涉及这个方面的内容,所以最近研究了一下 这里把我的一点小心得写给大家 希望大家讨论共同进步
首先我觉得这种功能的实现其实是依靠对数据包的解析 我的思路是把网站的整个源码先截取下来 然后对包的每一行HTML代码进行分析 找到需要的内容的HTML代码的共性 再提取这些共性代码出来 最后精简掉这些多余的代码得到需要内容 我写了一个粗略的程序来获取金山词霸的翻译 贴上让大家看看 顺便问达人这ENCODE的转化怎么完成 我想好久都没能解决 import java.net.*; import java.io.*;
public class gethead { public static String str = "<tr><td></td><td class=\"explain_attr\">int.</td></tr><tr><td><img width=\"1\" height=\"1\"></td><td class=\"explain_item\">"; public static String end = "</td></tr>"; URL url; URLConnection uc; InputStream in; DataInputStream din; String get; StringBuffer sb; StringBuffer add; DataInputStream in1; public gethead() { int i=0; sb = new StringBuffer(); add = new StringBuffer(); add.append("http://cb.kingsoft.com/search?s="); System.out.println("Input the word you want to get:"); try { in1 = new DataInputStream(System.in); String word = in1.readLine(); add.append(word).append("&lang=utf-8"); url = new URL(add.toString()); uc = url.openConnection(); in = uc.getInputStream(); din = new DataInputStream(in); while((get = din.readLine())!=null) { if((get.indexOf(str))!=-1) { i=i+1; } if(i!=0) { sb.append(get); } if(get.indexOf(end)!=1) { i=0; } } String oot = sb.toString(); oot = oot.replaceAll(str,""); oot = oot.replaceAll(end,""); //oot = java.net.URLEncoder.encode(oot); oot = new String(oot.getBytes("UTF-8"),"GB2312"); System.out.println(oot); }catch(IOException e) {}catch(Exception e) {} } public static void main(String[] args) { gethead g = new gethead(); } } 技术的核心是截取数据流,再进行分析提取,看起来还是比较简单的,但是难点在对HTML的源代码的具体分析和操作上 其实这个技术的应用我个人觉得还是会比较广泛的 在网络的数据流的分析应用上还是比较简单易用的

|
|
相关文章:相关软件: |
|