源码分享:蜘蛛抓取淘宝网上 买过某类商品的用户名

思路:通过淘宝搜索页链接到达每个商品页;用正则表达式匹配出买过商品用户名并处理分页

大家看个意思吧能运行但taobao购买者列表已经改成js没法直接采了


    =dp-j>
  1. =keyword>package org.jason.web.spider.tabao;   
  2.   
  3. =keyword>import java.io.BufferedReader;   
  4. =keyword>import java.io.InputStreamReader;   
  5. =keyword>import java.net.HttpURLConnection;   
  6. =keyword>import java.net.URL;   
  7. =keyword>import java.net.URLConnection;   
  8. =keyword>import java.util.ArrayList;   
  9. =keyword>import java.util.HashMap;   
  10. =keyword>import java.util.List;   
  11. =keyword>import java.util.Map;   
  12. =keyword>import java.util.regex.Matcher;   
  13. =keyword>import java.util.regex.Pattern;   
  14.   
  15.   
  16. =keyword>public =keyword> UserSpider {   
  17.        
  18.     =comment>//下区域正则表达式   
  19.     =keyword>private =keyword> =keyword>final String NEXT_PAGE==>"         <a href=\"(.*?)\" =\"page-next\"><span>下页</span></a>";   
  20.     =keyword>private =keyword> =keyword>final String LINK = =>"                                    <h3 =\"summary\"><a href=\"(.*?)\"  target=_blank  onclick=\"(.*?)\"  =\"EventCanSelect\">(.*?)</a></h3>";   
  21.     =keyword>private =keyword> =keyword>final String USER = =>"                                                <a href=\"http://space.taobao.com/(.*?)/portal/personal_portal.htm\" target=\"_blank\">(.*?)</a>";   
  22.        
  23.     =keyword>private  =keyword> =keyword>final String PAR= =>"?bid_page=1&page_size=100&is_start=true" ;   
  24.        
  25.     =keyword>private =keyword> =keyword>final =keyword> I_BREAK = =number>2;   
  26.        
  27.     =comment>/**  
  28. =comment>     * @param args  
  29. =comment>     */  
  30.     =keyword>public =keyword> =keyword>void (String args) {   
  31.            
  32.         Map<String,String> map = =keyword> HashMap<String,String>;   
  33.            
  34.         String s==>"http://search1.taobao.com/browse/0/n-g,nfyg6za----------------40--commend-0-all-0.htm?at_topsearch=1&ssid=e-s5";   
  35.         List<String> l = getLinks(s);   
  36.         .out.prln(=>"总链接数:"+l.size);   
  37.         =keyword>for(String m : l)   
  38.         {   
  39.             List<String> o = getPages(m);   
  40.             .out.prln(=>"页面数:"+o.size);   
  41.             =keyword>for(String page : o)   
  42.             {   
  43.                 List<String> u = getUsers(page);   
  44.                    
  45.                 .out.prln(=>"用户数:"+u.size);   
  46.                 =keyword>for(String user : u){   
  47.                     =keyword>(map.get(user)=keyword>null){   
  48.                         map.put(user, user);   
  49.                     }   
  50.                 }   
  51.             }   
  52.         }   
  53.            
  54.         =keyword>for(Map.Entry<String, String> entryTemp : map.entrySet){   
  55.             .out.prln(entryTemp.getKey);   
  56.             =comment>//保存用户...   
  57.         }   
  58.            
  59.     }   
  60.   
  61.     =keyword>private =keyword> List<String> getPages(String sUrl)   
  62.     {   
  63.         List<String> m = =keyword> ArrayList<String>;   
  64.         String sText = readURL(sUrl,=keyword>false,.getProperty(=>"line.separator"));   
  65.         m = replaceAll(sText,LINK,=number>1);   
  66.         =keyword> m;   
  67.     }   
  68.        
  69.        
  70.     =comment>/**  
  71. =comment>     * 取得页面内用户名  
  72. =comment>     * @param sUrl  
  73. =comment>     * @  
  74. =comment>     */  
  75.     =keyword>private =keyword> List<String> getUsers(String sUrl)   
  76.     {   
  77.         List<String> m = =keyword> ArrayList<String>;   
  78.         sUrl= sUrl+PAR;   
  79.         =comment>//.out.prln("页面:"+sUrl);   
  80.         String sText = readURL(sUrl,=keyword>false,.getProperty(=>"line.separator"));   
  81.         m = replaceAll(sText,USER,=number>2);   
  82.         =keyword> m;   
  83.     }   
  84.   
  85.     =comment>/**  
  86. =comment>     * 取得所有分页链接  
  87. =comment>     * @param baseUrl  
  88. =comment>     * @  
  89. =comment>     */  
  90.     =keyword>private =keyword> List<String> getLinks(String baseUrl)   
  91.     {   
  92.         List<String> s ==keyword> ArrayList<String>;   
  93.            
  94.         =keyword> (=>"".equals(baseUrl) || baseUrl=keyword>null){   
  95.             =keyword> s;   
  96.         }   
  97.            
  98.         String cur_url = baseUrl;   
  99.         s.add(cur_url);   
  100.         =keyword> i==number>0;   
  101.         String sText= =>"";   
  102.         =keyword>while(=keyword>true)   
  103.         {   
  104.                
  105.             sText = readURL(cur_url,=keyword>false,.getProperty(=>"line.separator"));   
  106.                
  107.             =keyword>(=>"".equals(sText))   
  108.             {   
  109.                 =keyword>;   
  110.             }   
  111.                
  112.             cur_url==>"";   
  113.                
  114.             cur_url = replace(sText,NEXT_PAGE,=number>1);   
  115.                
  116.             =keyword>(=>"".equals(cur_url))   
  117.             {   
  118.                 =keyword>;   
  119.             }   
  120.                
  121.             s.add(cur_url);   
  122.                
  123.                
  124.             =comment>//防止死循环   
  125.             i;   
  126.             =keyword>(i>I_BREAK)   
  127.             {   
  128.                 =keyword>;   
  129.             }   
  130.         }   
  131.            
  132.         =keyword> s;   
  133.     }   
  134.   
  135.     =comment>// 得到正则表达式,所匹配内容   
  136.     =keyword>public =keyword> String replace(String str, String pattern, =keyword> place)   
  137.     {   
  138.         String result = =>"";   
  139.         =keyword> (str=keyword>null || =>"".equals(str))   
  140.             =keyword> result;   
  141.         =keyword>  
  142.         {   
  143.             =keyword>try  
  144.             {   
  145.                 Pattern p = compile(pattern, =number>2);   
  146.                 Matcher m = p.matcher(str);   
  147.                 =keyword> (m.find)   
  148.                     result = m.group(place);   
  149.             }   
  150.             =keyword>catch (Exception ex)   
  151.             {   
  152.                 ex.prStackTrace;   
  153.             }   
  154.   
  155.             =keyword> result;   
  156.         }   
  157.     }   
  158.   
  159.        
  160.     =keyword>public =keyword> List<String> replaceAll(String str, String pattern,=keyword> i)   
  161.     {   
  162.         List<String> result = =keyword> ArrayList <String>;   
  163.            
  164.         =keyword> (str=keyword>null || =>"".equals(str))   
  165.             =keyword> result;   
  166.         =keyword>  
  167.         {   
  168.             =keyword>try  
  169.             {   
  170.                 Pattern p = Pattern.compile(pattern);   
  171.                 Matcher m = p.matcher(str);   
  172.                 =keyword>while (m.find){   
  173.                     result.add(m.group(i));   
  174.                 }   
  175.             }   
  176.             =keyword>catch (Exception ex)   
  177.             {   
  178.                 ex.prStackTrace;   
  179.             }   
  180.   
  181.             =keyword> result;   
  182.         }   
  183.     }      
  184.        
  185.        
  186.     =keyword>public =keyword> Pattern compile(String pattern, =keyword> mode)   
  187.     {   
  188.         =keyword> Pattern.compile(pattern, mode);   
  189.     }      
  190.        
  191.     =keyword>public =keyword> String readURL(String url, =keyword>boolean isPost, String line)   
  192.     {   
  193.         BufferedReader bufferedReader;   
  194.         StringBuffer sBuffer = =keyword> StringBuffer;   
  195.         =keyword>try  
  196.         {   
  197.             URL urlPath = =keyword> URL(url);   
  198.             URLConnection urlConnection = urlPath.openConnection;   
  199.             HttpURLConnection httpURL = (HttpURLConnection) urlConnection;   
  200.                
  201.             =keyword> (isPost) httpURL.RequestMethod(=>"POST");   
  202.   
  203.             =keyword>try  
  204.             {   
  205.                 httpURL.connect;   
  206. =comment>//                .out.prln("内容类型: "+httpURL.getContentType);    
  207. =comment>//                .out.prln("内容编码: "+httpURL.getContentEncoding);    
  208. =comment>//                .out.prln("内容长度: "+httpURL.getContentLength);    
  209. =comment>//                .out.prln("创建日期: "+ Date(httpURL.getDate));    
  210. =comment>//                .out.prln("最后修改日期: "+ Date(httpURL.getLastModied));    
  211. =comment>//                .out.prln("终止日期: "+ Date(httpURL.getExpiration));                    
  212.             }   
  213.             =keyword>catch (Exception e)   
  214.             {   
  215.                 e.prStackTrace;   
  216.             }   
  217.                
  218.             =keyword> httpResult = httpURL.getResponseCode;   
  219.             =keyword> (httpResult  HttpURLConnection.HTTP_OK)   
  220.             {   
  221.                    
  222.                 bufferedReader = =keyword> BufferedReader(=keyword> InputStreamReader(httpURL.getInputStream));   
  223.                 String sContent;   
  224.                    
  225.                 =keyword>while ((sContent = bufferedReader.readLine) != =keyword>null)   
  226.                 {   
  227.                     sBuffer.append(sContent).append(line);   
  228.                 }   
  229.   
  230.                 bufferedReader.close;   
  231.             }   
  232.                
  233.             httpURL.disconnect;   
  234.         }   
  235.         =keyword>catch (Exception ex)   
  236.         {   
  237.             ex.prStackTrace;   
  238.         }   
  239. =comment>//   
  240.         =keyword> sBuffer.toString;   
  241.     }      
  242.        
  243.        
  244. }  
Tags: 

延伸阅读

最新评论

发表评论