Chinaunix首页 | 论坛 | 博客
  • 博客访问: 213922
  • 博文数量: 53
  • 博客积分: 2626
  • 博客等级: 少校
  • 技术积分: 509
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-27 22:40
文章分类

全部博文(53)

文章存档

2012年(2)

2011年(13)

2010年(10)

2009年(28)

我的朋友

分类: 系统运维

2009-10-02 15:02:33

public class Access implements Runnable{
    
    HttpURLConnection huc; 
    InputStream is;
    BufferedReader reader;
    String url;
    
    public Access(){
        try {
            url="";
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            huc=(HttpURLConnection)new URL(url).openConnection();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        new Thread(this).start();
    }

    public void run() {
        try {
            huc.setRequestMethod("GET");
        } catch (ProtocolException e) {
            e.printStackTrace();
        }
        try {
            huc.setUseCaches(true);
            huc.connect();
            
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            is=huc.getInputStream();
            reader=new BufferedReader(new InputStreamReader(is,huc.getContentType().equals("text-html; charset=gb2312")?"gb2312":"UTF-8"));
            StringBuffer temp=new StringBuffer();
            String str;
            while((str=reader.readLine())!=null){
                temp.append(str+"\n");
            }
            System.out.println(new String(temp));
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            try {
                reader.close();
                is.close();
                huc.disconnect();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    
    
}


该爬虫设计的关键:
1.control,交互界面,对爬虫的控制
2.analysis HTML,对HTML进行分析,从中提取心得hot link.
3.多线程.并发抓取页面
阅读(4377) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~