用java抓取cnproxy代理服务器地址

[java]  view plain copy print ?
  1. package org.mingyuan.fetcher;     
  2. import java.io.BufferedOutputStream;     
  3. import java.io.BufferedReader;     
  4. import java.io.File;     
  5. import java.io.FileOutputStream;     
  6. import java.io.IOException;     
  7. import java.io.InputStreamReader;     
  8. import java.net.URL;     
  9. import java.util.ArrayList;     
  10. import java.util.HashMap;     
  11. import java.util.Set;     
  12. public class StringTest {     
  13.     /**   
  14.      * 通过url,得到url源文件   
  15.      * @param url   
  16.      * @return   
  17.      * @throws IOException   
  18.      */    
  19.     private static String getURLContent(String url) throws IOException {     
  20.         URL ipListUrl = new URL(url);     
  21.         BufferedReader in = new BufferedReader(new InputStreamReader(ipListUrl     
  22.                 .openStream()));     
  23.         String str = null;     
  24.         String html = "";     
  25.         while ((str = in.readLine()) != null) {     
  26.             html += str;     
  27.         }     
  28.         in.close();     
  29.         return html;     
  30.     }     
  31.     /**   
  32.      *    
  33.      * @param htmlContent   
  34.      * @param fileName   
  35.      * @return ip地址和ip地理信息   
  36.      * @throws IOException   
  37.      */    
  38.     private static HashMap<String,String> getIpList(String htmlContent,String fileName) throws IOException {     
  39.         String str1[] = htmlContent     
  40.                 .split("<tr><td width=/"140/">IP:Port</td><td width=/"40/">Type</td><td width=/"90/">Speed</td><td width=/"160/"> Country/Area</td></tr>");     
  41.         String str2 = str1[1];// ip及余下部分     
  42.         String str3[] = str2     
  43.                 .split("</table>");     
  44.         String str4 = str3[0];// <tr><td>24.25.26.128<SCRIPT type=text/javascript>document.write(":"+q+d)</SCRIPT></td><td>HTTP</td><td>296,984,984</td><td>美国 维吉尼亚州</td></tr>     
  45.              
  46.         String str5[]=str4.split("<tr><td>");     
  47.         HashMap<String,String> map=new HashMap<String,String>();     
  48.         int len=str5.length;     
  49.         String forIPArray[];     
  50.         String forAddrArray[];     
  51.         for(int i=0;i<len;i++){     
  52.             forIPArray=str5[i].split("<");     
  53.             forAddrArray=str5[i].split("<td>");     
  54.             int len1=forAddrArray.length;     
  55.             String str=forAddrArray[len1-1];     
  56.             String addrBeforeTD[]=str.split("</td></tr>");     
  57.             map.put(forIPArray[0], addrBeforeTD[0]);     
  58.         }     
  59.              
  60.              
  61.         /*File file = new File(fileName);   
  62.         FileOutputStream out = new FileOutputStream(file);   
  63.         BufferedOutputStream os = new BufferedOutputStream(out);   
  64.         Set<String> ipSet=map.keySet();   
  65.         for(String ip:ipSet){   
  66.             String addr=map.get(ip);   
  67.             os.write((ip+"     "+addr+"</br>").getBytes());   
  68.         }   
  69.         os.close();   
  70.         out.close();*/    
  71.         return map;     
  72.     }     
  73.     /**   
  74.      * 根据ip.cn数据库,获得ip地理信息   
  75.      * @param ipList   
  76.      * @throws IOException   
  77.      */    
  78.     private static void showAddress(ArrayList<String> ipList) throws IOException {     
  79.         File file = new File("c:/IpAddrInfo.html");     
  80.         FileOutputStream out = new FileOutputStream(file);     
  81.         BufferedOutputStream os = new BufferedOutputStream(out);     
  82.              
  83.         for(String ip:ipList){     
  84.             URL ipListUrl = new URL(     
  85.                     "http://www.ip.cn/getip.php?action=queryip&ip_url="+ip);     
  86.             BufferedReader in = new BufferedReader(new InputStreamReader(ipListUrl     
  87.                     .openStream()));     
  88.             String str = null;     
  89.             while ((str = in.readLine()) != null) {     
  90.                 os.write((str+"</br>").getBytes());     
  91.             }     
  92.             in.close();     
  93.         }     
  94.         os.close();     
  95.         out.close();     
  96.     }     
  97.     /**   
  98.      * @param args   
  99.      * @throws IOException   
  100.      */    
  101.     public static void main(String[] args) throws IOException {     
  102.         // TODO Auto-generated method stub     
  103.         String html=null;     
  104.         String htmlContent;     
  105.         HashMap<String,String> map=new HashMap<String,String>();     
  106.         for(int i=1;i<13;i++){     
  107.             if(i<11){     
  108.                 html="http://www.cnproxy.com/proxy"+i+".html";     
  109.                      
  110.             }else{     
  111.                 html="http://www.cnproxy.com/proxyedu"+(i-10)+".html";     
  112.             }     
  113.             htmlContent=getURLContent(html);     
  114.             map.putAll(getIpList(htmlContent,"c:/ip"+i+".html"));     
  115.         }     
  116.         File file = new File("c:/allIP.txt");     
  117.         FileOutputStream out = new FileOutputStream(file);     
  118.         BufferedOutputStream os = new BufferedOutputStream(out);     
  119.         Set<String> ipSet=map.keySet();     
  120.         int count=0;     
  121.         for(String ip:ipSet){     
  122.             os.write((ip+"         "+map.get(ip)+"/r/n").getBytes());     
  123.             count++;     
  124.         }     
  125.         os.close();     
  126.         out.close();     
  127.         System.out.println("操作完成,共获取"+count+"个IP及其地址信息");     
  128.     }     
  129. }   
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值