免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
12下一页
最近访问板块 发新帖
查看: 28533 | 回复: 11

网络爬虫(源代码参考-高手进入)[多线程讨论] [复制链接]

论坛徽章:
0
发表于 2004-10-20 01:35 |显示全部楼层
public class Spider implements Runnable {
        private ArrayList urls; //URL列表
    private HashMap indexedURLs; //已经检索过的URL列表
    private int threads ; //初始化线程数   
    public static void main(String argv[]) throws Exception {
         if(argv[0] == null){
           System.out.println("Missing required argument: [Sit URL]";
           return ;
         }
                Spider Spider = new Spider(argv[0]);
                Spider.go();
    }
    public Spider(String strURL) {
            urls    = new ArrayList();
        threads = 10;
        urls.add(strURL);
        threadList = new ArrayList();
        indexedURLs = new HashMap();

        if (urls.size() == 0)
            throw new IllegalArgumentException("Missing required argument: -u [start url]";
        if (threads < 1)
            throw new IllegalArgumentException("Invalid number of threads: " +
                threads);
    }
    public void go(String strURL) throws Exception {
        // index each entry point URL
        long start = System.currentTimeMillis();
        for (int i = 0; i < threads; i++) {
            Thread t = new Thread(this, "Spide " + (i+1));
            t.start();
            threadList.add(t);
        }
        while (threadList.size() >; 0) {
            Thread child = (Thread)threadList.remove(0);
            child.join();
        }
        long elapsed = System.currentTimeMillis() - start;
    }
    public void run() {
        String url;
        try {
            while ((url = dequeueURL()) != null) {
                indexURL(url);
            }
        }catch(Exception e) {
                logger.info(e.getMessage());
        }        
    }
    //检测URL列表容器中有没有URL没有被解析,如果有则返回URL由线程继续执行
   
    public synchronized String dequeueURL() throws Exception {
        while (true) {
            if (urls.size() >; 0) {
                return (String)urls.remove(0);
            }else {
                threads--;
                if (threads >; 0) {
                    wait();
                    threads++;
                }else {
                    notifyAll();
                    return null;
                }
            }
        }
    }
    /*
     * 添加URL和当前URL的级数,并唤醒睡眠线程     
     */
    public synchronized void enqueueURL(String url,int level) {
        if (indexedURLs.get(url) == null) {
            urls.add(url);
            indexedURLs.put(url, new Integer(level));
            notifyAll();
        }
    }
    /**
     * 通过URL解析出网页内容并解析出页面上的URL
     * @param url 页面链接
     * @throws java.lang.Exception
     */
    private void indexURL(String url) throws Exception {
        boolean flag = true ;
       //判断网页链接的级别,系统默认为三级
        int level = 1 ;
        if (indexedURLs.get(url) == null) {
           indexedURLs.put(url, new Integer(level));
        }else{
           level = ((Integer)indexedURLs.get(url)).intValue();
           //只检测到页面的第二级
           if(level >; 2 )
             return ;
           level++ ;
        }

        String strBody = null ;
        try{
                //解析页面内容
                strBody = loadURL(url);
        }catch(Exception e){
                return ;
        }
        if (strBody != null) {
          String urlGroups[] = null ;
          try{
                  //解析出页面所以URL
                  urlGroups = parseURLs(summary);
          }catch(Exception e){
                  logger.info(e.getMessage());
          }
          if(urlGroups == null)
                  urlGroups = new String[0] ;
                  
          strBody = null ;
          for (int i = 0; i < urlGroups.length; i++) {
                enqueueURL(urlGroups,level);
          }
        }
    }

}


请各位高手评评,这里面有几个很大的问题,线程出现死锁、并且该程序占用系统cpu很高,请高手指出问题,并提出解决方案。沟通让人进步!!!!!!

论坛徽章:
0
发表于 2004-10-20 01:37 |显示全部楼层

网络爬虫(源代码参考-高手进入)[多线程讨论]

这个程序已经简化了些方法,因为这里主要是分析多线程的问题,所以我只是把涉及到线程部分方法罗列出来。

论坛徽章:
0
发表于 2004-10-20 12:03 |显示全部楼层

网络爬虫(源代码参考-高手进入)[多线程讨论]

怎么没有个高手进来啊,郁闷啊..............

论坛徽章:
0
发表于 2004-10-20 15:08 |显示全部楼层

网络爬虫(源代码参考-高手进入)[多线程讨论]

怎么没有个高手进来啊,郁闷啊..............

论坛徽章:
0
发表于 2004-10-20 21:26 |显示全部楼层

网络爬虫(源代码参考-高手进入)[多线程讨论]

你在终止线程的时候用的是什么方法啊?直接重载了stop()吗?
给的东西还是少点,没头没尾的。
我以前的时候也写过类似的“蜘蛛”程序,效果不错啊。而且也不会出现CPU占用很高或线程死锁啊!

论坛徽章:
0
发表于 2004-10-21 06:42 |显示全部楼层

网络爬虫(源代码参考-高手进入)[多线程讨论]

你用ArrayList定义urls,你知道ArrayList是线程安全的吗?
你的urls.get()和put()有同步锁吗?

论坛徽章:
0
发表于 2004-10-21 19:26 |显示全部楼层

网络爬虫(源代码参考-高手进入)[多线程讨论]

谢谢大家的留言,建议一下,留言的同志们,能否留个QQ,email,msn?
这样可以保证有更多的交流机会!

论坛徽章:
0
发表于 2004-10-21 19:39 |显示全部楼层

网络爬虫(源代码参考-高手进入)[多线程讨论]

我的msn:jhf2008_do@hotmail.com
QQ:273748358
希望多些交流!!

论坛徽章:
0
发表于 2004-10-23 12:04 |显示全部楼层

网络爬虫(源代码参考-高手进入)[多线程讨论]

怎么没有人回复啊

论坛徽章:
0
发表于 2004-10-23 12:05 |显示全部楼层

网络爬虫(源代码参考-高手进入)[多线程讨论]

大家踊跃回帖啊
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

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

清除 Cookies - ChinaUnix - Archiver - WAP - TOP