java 簡單網絡爬蟲實現

今天在羣裏有人討論到了網絡爬蟲原理,在此,我就寫了一個簡單的網絡爬蟲,由於時間倉促,存在很多不規範,望大家擔待,但基本原理,代碼中已經體現了。願大家學習開心。

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpCookie;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;


public class SocketScan {
       
        private static final int MAX_SIZE = 5;
        public static List<String> httpContextList = new ArrayList<String>();
       
        public static void main(String[] args) {
                // 得到網站URL,並讀取出來
                String httpContext = searchHttpContexts("http://10.125.2.36:8080/FileUpload/test.html");

                System.out.println("httpContext size: "+httpContextList.size());
               
                for (String string : httpContextList) {
                        System.out.println(string);
                        System.out.println();
                        System.out.println("分隔符==============================================================================");
                        System.out.println();
                }
               
        }

       
        private static List<String> GetURLByHttpContext(String httpContext) {
                List<String> urlList = new ArrayList<String>();
                String mark = "href=\"";
                int len = mark.length();
                int start = 0;
                int end = 0 ;
                while((start = httpContext.indexOf(mark,start))!=-1){
                        start = start + len;
                        end = httpContext.indexOf("\"",start);
                        urlList.add(httpContext.substring(start,end));
                }
                return urlList;
        }
       
       
        private synchronized static String searchHttpContexts(String urlPath) {
                try {
                        if(httpContextList.size() > MAX_SIZE){
                                return null;
                        }
                        String sb = getHttpContext(urlPath);
                        httpContextList.add(sb);
                       
                        List<String> urlList = GetURLByHttpContext(sb.toString());
                        if(urlList.size() >0){
                                for (String subUrl : urlList) {
                                        String subHttpContext = searchHttpContexts(subUrl);
                                        if(httpContextList.size() > MAX_SIZE){
                                                return null;
                                        }
                                        httpContextList.add(subHttpContext);
                                }
                        }
                        return sb;
                } catch (UnknownHostException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
                return null;
        }
       
       

        private static String getHttpContext(String urlPath)
                        throws MalformedURLException, IOException {
                URL url = new URL(urlPath);
                URLConnection conn = url.openConnection();
                BufferedInputStream input = new BufferedInputStream(conn.getInputStream());
                byte[] b = new byte[1024];
                int temp;
                StringBuilder sb = new StringBuilder();
                while ((temp = input.read(b)) != -1) {
                        String value = new String(b);
                        sb.append(value);
                }
                return sb.toString();
        }
       
       
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章