网页爬虫抓取URL简单实现

package com.ogilvy.sayes.util;

import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.Hashtable;

/* 
 Description:     爬网页用 
 Author     :     long.tang
 */

public class SearchCrawler {

	public String myGetHttpFile2(String url) {

		String urlSource = url;
		StringBuffer htmlBuffer = new StringBuffer();
		String returnStr = null;
		try {
			InputStream imageSource = new URL(urlSource).openStream();
			int ch;
			while ((ch = imageSource.read()) > -1) {
				htmlBuffer.append((char) ch);
			}
			imageSource.close();
			returnStr = new String(htmlBuffer);
			returnStr = new String(returnStr.getBytes("ISO8859_1"), "GBK");
		} catch (Exception e) {
			System.out.println("error>>>>");
			e.printStackTrace();
		}

		//System.out.println("@@@:" + returnStr);
		if (returnStr != null) {
			return returnStr;
		} else {
			return "nothing";
		}

	}

	public void doit(String content, int depth) throws Exception {
		
		depth--;
		if (depth < 1) {
			//System.out.println("break::::");
			return;
		}

		SearchCrawler search = new SearchCrawler();
		ArrayList list = new ArrayList();
		int j = 0;
		String start = "href=";
		String end = "\"";
		String url = "";
		String type = "http";
		String[] urls;
		while (content.indexOf(start, j) > -1) {

				url = content.substring(content.indexOf(start, j) + 6, content.indexOf(end, content.indexOf(start, j) + 6));//+6 href="
				if (url.indexOf(type) > -1) {
					if (url.indexOf(".css") == -1&&url.indexOf(".ico") == -1&&url.indexOf(".exe") == -1) {
						System.out.println(url);
						
						list.add(url);

						if (list != null && list.size() > 0) {

							for (int k = 0; k < list.size(); k++) {
								doit(search.myGetHttpFile2(String.valueOf(list.get(k))), depth);
						
							}

						}
					}

				}

			
			j = content.indexOf(start, j) + 1;
			
		}

	}

	public static void main(String arg[]) {

		SearchCrawler search = new SearchCrawler();
		try {
			search.doit(search.myGetHttpFile2("http://www.2345.com/"),3);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值