java之爬蟲

近期研究爬蟲爬取網站鏈接:
1.需要獲取所有超鏈接
2.排除已爬取的鏈接,去重
3.爬蟲的廣度和深度方向研究(ps:目前沒有研究徹底)
以下是實現代碼:

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.swing.plaf.synth.SynthSpinnerUI;

public class study {
    private static List<String> waitforUrl=new ArrayList<>();//存儲抓取出來的url,等待爬
    private static Set<String> goforUrl=new HashSet<>();//存儲爬過的url
    private static Map<String,Integer> allUrldepth=new HashMap<>();//對所有url進行爬去深度判斷
    private static int Maxdepth=2;
    public static void main(String[] args) {

        String urlstr="............";
        study.gourl(urlstr, 1);

    }

    public static void gourl(String urlstr,int depath) {
        if(!(goforUrl.contains(urlstr)||depath>Maxdepth)) {
            goforUrl.add(urlstr);
            try {
                URL url=new URL(urlstr);
                URLConnection urlConn=url.openConnection();//建立url鏈接
                InputStream is=urlConn.getInputStream();//通過鏈接獲取頁面內容,爲字節流
                InputStreamReader isr=new InputStreamReader(is,"utf-8");//將字節流轉化爲字符流
                BufferedReader br=new BufferedReader(isr);//讀取字節流

                StringBuffer sb=new StringBuffer();//實例化StringBuffer用來存儲讀取數據
                String line=null;

                while((line=br.readLine())!=null) {
                    sb.append(line);
                    //System.out.println(line);
                    Pattern p = Pattern.compile("<a .*href=.+</a>");
                    Matcher m=p.matcher(line);
                    while(m.find()) {
                        String href=m.group();
                        href=href.substring(href.indexOf("href="));
                        if(href.charAt(5)=='\"'||href.charAt(5)=='\''){
                            href=href.substring(6);
                          }else{
                            href=href.substring(5);
                          }
                        try {
                        href=href.substring(0,href.indexOf("\""));
                        }catch(Exception e) {
                            href=href.substring(0,href.indexOf("\'"));
                        }
                        waitforUrl.add(href);
                        allUrldepth.put(href, depath+1);
                    }
                }

                is.close();//關閉輸出流
                isr.close();//關閉字節流讀取
                br.close();
                System.out.println(urlstr);
                System.out.println("鏈接總數:"+waitforUrl.size()+",已爬去鏈接數:"+goforUrl.size());

            }catch(Exception e){
                 e.printStackTrace();
            }

        }
        //用遞歸的方法繼續爬取其他鏈接
            String nexturl=waitforUrl.get(0);
            waitforUrl.remove(0);
            gourl(nexturl,allUrldepth.get(nexturl));
    }

}

java之爬蟲
遇到的問題:allUrldepth作爲hashmap,當key值即url一樣時存在value值即depath被覆蓋問題
解決辦法:通過判斷hashmap裏key值是否包含,包含就把原有depath添加進去
if(allUrldepth.containsKey(href)) {
allUrldepth.put(href,depath);
}else {
allUrldepth.put(href, depath+1);
}

問題:通過一個線程執行速度太慢,通過多線程解決

解決辦法:

private static int MAX_THREAD=5;//記錄總線程數5條
private static Object obj=new Object();//Object對象,幫助進行線程的等待操作
public class myThread extends Thread{
        @Override
        public void run() 
        {
            while(true) 
            {
                if(waitforUrl.size()>0) 
                {
                    String url=waitforUrl.get(0);
                    waitforUrl.remove(0);
                    gourl(url, allUrldepth.get(url));
                }else 
                {
                    System.out.println("當前線程準備就緒,等待連接爬取:"+this.getName());
                      //建立一個對象,讓線程進入等待狀態,即wait()
                      synchronized(obj){
                        try{
                          obj.wait();
                        }catch(Exception e){

                        }

                }
            }
        }
    }

java之爬蟲

問題:由於截取的部分a標籤之間同時存在單雙引號,例如www.baidu.com' ><img src="....</a>

解決辦法:改變正則,只截取a標籤前半部分,<a .*href=.+?>使用非貪婪模式獲取

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