免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 7601 | 回复: 3
打印 上一主题 下一主题

如何用java实现抓取网页? [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-05-26 17:01 |只看该作者 |倒序浏览
大家好!我是一个Java菜鸟。
现在老师要求我们做一个抓取网页(比如说sohu首页)的程序,
我自己做的有好多毛病,老出错。
所以问问大家抓取一个网页怎么实现呢?

论坛徽章:
0
2 [报告]
发表于 2005-05-26 17:21 |只看该作者

如何用java实现抓取网页?

我不是很看得懂?是不是写上URL,然后将上面的所有内容全部down下来的那种吗?还是抓张图片?

论坛徽章:
0
3 [报告]
发表于 2005-05-26 17:27 |只看该作者

如何用java实现抓取网页?

我也是个菜鸟
但是我的解决方案是:
先写下URL
DOWN所有的HTML
根据HTML下所有的图片
分存为一个INDEX.HTM 和一个IMAGES文件夹
然后就能用了
不知道对不对呀

论坛徽章:
0
4 [报告]
发表于 2005-05-26 19:51 |只看该作者

如何用java实现抓取网页?

谢谢诸位的热心:)

偶已经解决啦,先发上来,给跟偶一样菜鸟的一起来共享:)

import java.net.*;
import java.io.*;

public class Catch1 {
     public void test(){
    StringBuffer document= new StringBuffer();
    try {
      URL url = new URL("http://www.sohu.com";
      URLConnection conn = url.openConnection();
      BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
      String line = null;
      while ((line = reader.readLine()) != null)
        document.append(line + "\n";
      reader.close();
    } catch (MalformedURLException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }

    //pzy add
    String str = document.toString();
    String strDir = "E:\\text";
    String strFile = "test.html";
    File myFile = new File(strDir, strFile);

    try {
      myFile.createNewFile();
      BufferedWriter bw = new BufferedWriter(
                            new FileWriter(myFile.getPath(), true));
      bw.write(str);
      bw.flush();
      bw.close();
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

     public static void main(String[] args){
      Catch1 catch2=new Catch1();
         catch2.test();
           }
}
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP