java取网页数据_浅析JAVA实现网页取内容

有很多网站提供从其他网站提取新闻甚至是从向翻译网站取内容

由于手头做的网站需要涉及这个方面的内容,所以最近研究了一下

这里把我的一点小心得写给大家

希望大家讨论共同进步

首先我觉得这种功能的实现其实是依靠对数据包的解析

我的思路是把网站的整个源码先截取下来

然后对包的每一行HTML代码进行分析

找到需要的内容的HTML代码的共性

再提取这些共性代码出来

最后精简掉这些多余的代码得到需要内容

我写了一个粗略的程序来获取金山词霸的翻译

贴上让大家看看

顺便问达人这ENCODE的转化怎么完成

我想好久都没能解决

import java.net.*;

import java.io.*;

public class gethead

{

        public static String str = "

int.";

public static String end = "

";

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的源代码的具体分析和操作上

其实这个技术的应用我个人觉得还是会比较广泛的

在网络的数据流的分析应用上还是比较简单易用的

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值