收藏 分享(赏)

网络爬虫源代码.doc

上传人:cjc2202537 文档编号:6357564 上传时间:2019-04-09 格式:DOC 页数:4 大小:23KB
下载 相关 举报
网络爬虫源代码.doc_第1页
第1页 / 共4页
网络爬虫源代码.doc_第2页
第2页 / 共4页
网络爬虫源代码.doc_第3页
第3页 / 共4页
网络爬虫源代码.doc_第4页
第4页 / 共4页
亲,该文档总共4页,全部预览完了,如果喜欢就下载吧!
资源描述

1、网络爬虫源代码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(argv0 = null)System.out.println(“Missing required argument: Sit URL“);return ;Spider Spider

2、= new Spider(argv0);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 ; 0) Thread child =

3、 (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 dequeu

4、eURL() 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)

5、;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(l

6、evel);elselevel = (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/解析出页面所以 URLurlGroups = parseURLs(summary);catch(Exception e)logger.info(e.getMessage();if(urlGroups = null)urlGroups = new String0 ;strBody = null ;for (int i = 0; i urlGroups.length; i+) enqueueURL(urlGroupsi,level);

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > 网络科技 > 网络与通信

本站链接:文库   一言   我酷   合作


客服QQ:2549714901微博号:道客多多官方知乎号:道客多多

经营许可证编号: 粤ICP备2021046453号世界地图

道客多多©版权所有2020-2025营业执照举报