【多線程數據採集】使用Jsoup抓取數據+破解屏蔽ip訪問。

java抓取數據+破解屏蔽ip訪問 html

今天就講解一下,怎麼破解 服務器 屏蔽ip的請求。 java

如今大多網站採起 ip訪問次數達到必定次數就屏蔽ip的功能。 apache

那麼要破解服務器的屏蔽。 就只有改變ip, 或者代理ip。 服務器

若是用代理,哪裏去找那麼多ip呢。  用adsl 獲取動態ip不是很簡單嗎。 多線程

轉載註明出處:http://blog.csdn.net/column/details/threadgrab.html app


那麼如今就貼上adsl獲取動態ip的方案實例源碼 工具


一、抓取網頁數據的時候  catch異常以後 , 就採起撥號程序 網站

[java] view plain copy
  1. //門票瀏覽  url參數 http://www.lvmama.com/dest/lantiancheng  
  2.     public static DataBean getWebData1(String url){  
  3.         DataBean data = null;  
  4.         try {  
  5.             Document docdata = Jsoup.connect(url).timeout(20000).get();  
  6.               
  7.         } catch (Exception e) {  
  8.             e.printStackTrace();  
  9.                         //撥號一下  
  10.                         ConnectAdslNet.reconnectAdsl("寬帶",Main.adslname,Main.adslpass);  
  11.         }  
  12.         return data;  
  13.     }  


二、撥號獲取動態ip的 工具類 ui

[java] view plain copy
  1. import java.io.BufferedReader;  
  2. import java.io.InputStreamReader;  
  3.   
  4. import org.apache.log4j.Logger;  
  5.   
  6. /** 
  7.  *  
  8.  * ADSL撥號上網 
  9.  * Windwos操做系統須要是GBK編碼 
  10.  * @author yijianfeng 
  11.  *  
  12.  */  
  13.   
  14. public class ConnectAdslNet {  
  15.     static Logger logger = Logger.getLogger(ConnectAdslNet.class);  
  16.       
  17.      /**  
  18.      * 執行CMD命令,並返回String字符串  
  19.      */    
  20.     public static String executeCmd(String strCmd) throws Exception {    
  21.         Process p = Runtime.getRuntime().exec("cmd /c " + strCmd);    
  22.         StringBuilder sbCmd = new StringBuilder();    
  23.           
  24.         //注意編碼 GBK  
  25.         BufferedReader br = new BufferedReader(new InputStreamReader(p    
  26.                 .getInputStream(),"GBK"));    
  27.         String line;    
  28.         while ((line = br.readLine()) != null) {    
  29.             sbCmd.append(line + "\n");    
  30.         }    
  31.         return sbCmd.toString();    
  32.     }    
  33.     
  34.     /**  
  35.      * 鏈接ADSL  
  36.      */    
  37.     public static boolean connectAdsl(String adslTitle, String adslName, String adslPass) throws Exception {    
  38.         System.out.println("正在創建鏈接.");    
  39.         String adslCmd = "rasdial " + adslTitle + " " + adslName + " "    
  40.                 + adslPass;    
  41.         String tempCmd = executeCmd(adslCmd);    
  42.           
  43.         // 判斷是否鏈接成功    
  44.         if (tempCmd.indexOf("已鏈接") > 0) {    
  45.             System.out.println("已成功創建鏈接.");    
  46.             return true;    
  47.         } else {    
  48.             System.out.println(tempCmd);    
  49.             System.out.println("創建鏈接失敗");    
  50.             return false;    
  51.         }    
  52.     }    
  53.     
  54.     /**  
  55.      * 斷開ADSL  
  56.      */    
  57.     public static boolean disconnectAdsl(String adslTitle) throws Exception {    
  58.         String disconnectAdsl = "rasdial " + adslTitle + " /disconnect";    
  59.         String result = executeCmd(disconnectAdsl);         
  60.            
  61.         if (result.indexOf("沒有鏈接")!=-1){    
  62.             System.out.println(adslTitle + "鏈接不存在!");    
  63.             return false;    
  64.         } else {    
  65.             System.out.println("鏈接已斷開");    
  66.             return true;    
  67.         }    
  68.     }    
  69.       
  70.     /** 
  71.      * adsl從新撥號,支持失敗不斷重撥 
  72.      * @param args 
  73.      * @throws Exception 
  74.      */  
  75.     public static boolean reconnectAdsl(String adslTitle, String adslName, String adslPass){  
  76.         boolean bAdsl = false;  
  77.         try {  
  78.             disconnectAdsl(adslTitle);   
  79.             Thread.sleep(3000);           
  80.             bAdsl = connectAdsl(adslTitle,adslName,adslPass);  
  81.             Thread.sleep(3000);  
  82.             int i = 0;  
  83.             while (!bAdsl){  
  84.                 disconnectAdsl(adslTitle);   
  85.                 Thread.sleep(3000);  
  86.                 bAdsl = connectAdsl(adslTitle,adslName,adslPass);  
  87.                 Thread.sleep(3000);  
  88.                 if(i>5){  
  89.                     break;  
  90.                 }  
  91.             }  
  92.         }catch(Exception e){  
  93.             logger.error("ADSL撥號異常:", e);  
  94.         }  
  95.           
  96.         return bAdsl;         
  97.     }  
  98.        
  99.     public static void main(String[] args) throws Exception {  
  100. //        reconnectAdsl("寬帶","adsl帳號","密碼");    
  101.     }     
  102.       
  103. }  


採用上述辦法,基本上就能夠解決撥號的問題了。 編碼


若是程序加入了多線程。 那麼就必須考慮多線程,撥號同步,以及數據同步問題。  提升效率和避免重複操做。


到此,破解屏蔽ip訪問就搞定了!

相關文章
相關標籤/搜索