1. /**  
  2.  * 内容摘要:采集工具类 
  3.  * 流程说明: 
  4.  * @author wanghao   QQ:115308504 
  5.  * @return  
  6.  */ 
  7. package util; 
  8.  
  9. import java.util.regex.Matcher; 
  10. import java.util.regex.Pattern; 
  11.  
  12. import org.apache.http.HttpEntity; 
  13. import org.apache.http.HttpResponse; 
  14. import org.apache.http.client.HttpClient; 
  15. import org.apache.http.client.methods.HttpGet; 
  16. import org.apache.http.impl.client.DefaultHttpClient; 
  17. import org.apache.http.util.EntityUtils; 
  18. import org.htmlparser.Node; 
  19. import org.htmlparser.Parser; 
  20. import org.htmlparser.tags.ScriptTag; 
  21. import org.htmlparser.tags.StyleTag; 
  22. import org.htmlparser.visitors.ObjectFindingVisitor; 
  23. import org.htmlparser.visitors.TextExtractingVisitor; 
  24.  
  25. public class CrawNewsTools { 
  26.  
  27.     // 使用HttpClient组件读取指定URL的页面HTML源码 
  28.     public static String getPage(String url, String encode) { 
  29.         String page = ""
  30.         HttpClient httpClient = null
  31.         try { 
  32.             httpClient = new DefaultHttpClient(); 
  33.             // 创建httpget 
  34.             HttpGet httpget = new HttpGet(url); 
  35.             System.out.println("请求URI路径:" + httpget.getURI()); 
  36.             // 执行get请求 
  37.             HttpResponse response = httpClient.execute(httpget); 
  38.             // 获得响应实体 
  39.             HttpEntity httpEntity = response.getEntity(); 
  40.             String charset = EntityUtils.getContentCharSet(httpEntity); 
  41.             System.out.println("###当前页面编码:" + charset); 
  42.             // 获取内容时,指定编码 
  43.             if (encode != null && !encode.trim().equals("")) { 
  44.                 System.out.println("###采用指定编码:" + encode); 
  45.             } else if (charset != null) { 
  46.                 System.out.println("###采用网页自身编码:" + charset); 
  47.                 encode = charset
  48.             } else { 
  49.                 System.out.println("###采用默认UTF-8编码:UTF-8"); 
  50.                 encode = "UTF-8"
  51.             } 
  52.             page = EntityUtils.toString(httpEntity, encode); 
  53.             System.out.println(page); 
  54.             page = removeCssTag(page, encode); 
  55.             page = removeJsTag(page, encode); 
  56.             page = getBody(page); 
  57.             //page = formatHtml(page); 
  58.         } catch (Exception ex) { 
  59.             ex.printStackTrace(); 
  60.         } finally { 
  61.             httpClient.getConnectionManager().shutdown(); 
  62.         } 
  63.         return page; 
  64.     } 
  65.  
  66.     // 格式化body标签 
  67.     public static String fromatBodyTag(String htmlCode) { 
  68.         String result = htmlCode
  69.         if (htmlCode != null) { 
  70.             while (result.indexOf("<BODY") != -1) { 
  71.                 result.replaceAll("<BODY", "<body"); 
  72.             } 
  73.             while (result.indexOf("<Body") != -1) { 
  74.                 resultresult = result.replaceAll("<Body", "<body"); 
  75.             } 
  76.             while (result.indexOf("</BODY") != -1) { 
  77.                 resultresult = result.replaceAll("</BODY", "</body"); 
  78.             } 
  79.             while (result.indexOf("</Body") != -1) { 
  80.                 resultresult = result.replaceAll("</Body", "</body"); 
  81.             } 
  82.         } 
  83.         return result; 
  84.     } 
  85.  
  86.     // 使用正则表达式提取body体内容 
  87.     public static String getBody(String htmlCode) { 
  88.         if (htmlCode == null) { 
  89.             return null; 
  90.         } 
  91.         Pattern pattern = Pattern.compile("<body(.*)>(.*)</body>", 
  92.                 Pattern.MULTILINE | Pattern.DOTALL); 
  93.         Matcher matcher = pattern.matcher(htmlCode); 
  94.         if (matcher.find()) { 
  95.             return matcher.group(); 
  96.         } else { 
  97.             return null; 
  98.         } 
  99.     } 
  100.  
  101.     // 过滤css标签 
  102.     public static String removeCssTag(String htmlCode, String encode) { 
  103.         String htmlEndCode = htmlCode
  104.         try { 
  105.             Parser parser = Parser.createParser(htmlCode, encode); 
  106.             ObjectFindingVisitor visitor = new ObjectFindingVisitor( 
  107.                     StyleTag.class); 
  108.             parser.visitAllNodesWith(visitor); 
  109.             Node[] nodes = visitor.getTags(); 
  110.             for (int i = 0; i < nodes.length; i++) { 
  111.                 // System.out.println(nodes[i].toHtml()); 
  112.                 htmlEndCodehtmlEndCode = htmlEndCode.replace(nodes[i].toHtml(), ""); 
  113.             } 
  114.             // System.out.println("###去除css标签后:" + htmlEndCode); 
  115.         } catch (Exception e) { 
  116.             e.printStackTrace(); 
  117.         } 
  118.         return htmlEndCode; 
  119.     } 
  120.  
  121.     // 过滤js标签 
  122.     public static String removeJsTag(String htmlCode, String encode) { 
  123.         String htmlEndCode = htmlCode
  124.         try { 
  125.             Parser parser = Parser.createParser(htmlCode, encode); 
  126.             ObjectFindingVisitor visitor = new ObjectFindingVisitor( 
  127.                     ScriptTag.class); 
  128.             parser.visitAllNodesWith(visitor); 
  129.             Node[] nodes = visitor.getTags(); 
  130.             for (int i = 0; i < nodes.length; i++) { 
  131.                 // System.out.println(nodes[i].toHtml()); 
  132.                 htmlEndCodehtmlEndCode = htmlEndCode.replace(nodes[i].toHtml(), ""); 
  133.             } 
  134.             // System.out.println("###去除js标签后:" + htmlEndCode); 
  135.         } catch (Exception e) { 
  136.             e.printStackTrace(); 
  137.         } 
  138.         return htmlEndCode; 
  139.     } 
  140.  
  141.     // 格式化指定的HTML源码 
  142.     public static String formatHtml(String htmlcode) { 
  143.         String result = htmlcode
  144.         if (htmlcode != null && htmlcode.trim().length() > 0) { 
  145.             // 去除回车符 
  146.             while (result.indexOf("\r") != -1) { 
  147.                 resultresult = result.replaceAll("\r", ""); 
  148.             } 
  149.             // 去除换行符 
  150.             while (result.indexOf("\n") != -1) { 
  151.                 resultresult = result.replaceAll("\n", ""); 
  152.             } 
  153.             // 去除制表符 
  154.             while (result.indexOf("\t") != -1) { 
  155.                 resultresult = result.replaceAll("\t", ""); 
  156.             } 
  157.             // 去除多余空格 
  158.             while (result.indexOf("  ") != -1) { 
  159.                 resultresult = result.replaceAll("  ", " "); 
  160.             } 
  161.             // 去除全角空格 
  162.             while (result.indexOf(" ") != -1) { 
  163.                 resultresult = result.replaceAll(" ", ""); 
  164.             } 
  165.             return result; 
  166.         } else { 
  167.             return null; 
  168.         } 
  169.     } 
  170.  
  171.     // 使用HtmlParser组件去除内容中的HTML标签,得到纯文本内容 
  172.     public static String getText(String content, String encode) { 
  173.         String result = content
  174.         try { 
  175.             Parser parser = Parser.createParser(content, encode); 
  176.             // 创建TextExtractingVisitor对象 
  177.             TextExtractingVisitor visitor = new TextExtractingVisitor(); 
  178.             // 去除网页中的所有标签,提出纯文本内容 
  179.             parser.visitAllNodesWith(visitor); 
  180.             result = visitor.getExtractedText(); 
  181.             // System.out.println("###去除HTML标签:" + result); 
  182.         } catch (Exception ex) { 
  183.             ex.printStackTrace(); 
  184.         } 
  185.         return result; 
  186.     } 
  187.  
  188.     // 查询网页包含某种标签的集合数组 
  189.     @SuppressWarnings("unchecked") 
  190.     public static Node[] getTagList(String htmlCode, String encode, Class t) { 
  191.         Node[] nodes = null
  192.         try { 
  193.             Parser parser = Parser.createParser(htmlCode, encode); 
  194.             ObjectFindingVisitor visitor = new ObjectFindingVisitor(t); 
  195.             parser.visitAllNodesWith(visitor); 
  196.             nodes = visitor.getTags(); 
  197.         } catch (Exception e) { 
  198.             e.printStackTrace(); 
  199.         } 
  200.         return nodes; 
  201.     } 
  202.  
  203.     // 判断自定字符串是否符号某种正则规则 
  204.     public static boolean isRegex(String isRegexString, String regexString) { 
  205.         boolean regexStatus = Pattern.matches(regexString, isRegexString); 
  206.         return regexStatus; 
  207.     } 

 

  1. /**  
  2.  * 内容摘要:蛋花儿网图片采集类 
  3.  * 流程说明: 
  4.  * @author wanghao   QQ:115308504 
  5.  * @return  
  6.  */ 
  7. package craw; 
  8.  
  9. import java.io.File; 
  10. import java.io.FileOutputStream; 
  11. import java.io.InputStream; 
  12. import java.net.URL; 
  13.  
  14. import org.htmlparser.Node; 
  15. import org.htmlparser.tags.ImageTag; 
  16.  
  17. import util.CrawNewsTools; 
  18.  
  19. public class CrawPicForDanHuaer { 
  20.  
  21.     // 采集图片 
  22.     public void crawImage(String url, String encode) { 
  23.         // 采集源码 
  24.         String page = CrawNewsTools.getPage(url, encode); 
  25.         System.out.println("当前采集页源码:" + page); 
  26.         Node[] nodes = CrawNewsTools.getTagList(page, encode, ImageTag.class); 
  27.         System.out.println(nodes.length); 
  28.         for (Node node : nodes) { 
  29.             String picUrl = ((ImageTag) node).getImageURL(); 
  30.             downloadPic(picUrl); 
  31.         } 
  32.     } 
  33.  
  34.     public void downloadPic(String picUrl) { 
  35.         File d = new File("c:\\danhuaer"); 
  36.         if (!d.exists()) { 
  37.             try { 
  38.                 d.mkdirs(); 
  39.             } catch (Exception e) { 
  40.                 e.printStackTrace(); 
  41.             } 
  42.         } 
  43.         String fileName = picUrl.substring(picUrl.lastIndexOf("/") + 1); 
  44.         if (!"touchad.gif".equals(fileName)) { 
  45.             try { 
  46.                 URL url = new URL(picUrl); 
  47.                 int fileSize = url.openConnection().getContentLength(); 
  48.                 if (fileSize >= 1024 * 20) { 
  49.                     String filepath = "c:\\danhuaer\\danhuaer#" + fileName; 
  50.                     File f = new File(filepath); 
  51.                     FileOutputStream fos = new FileOutputStream(f); 
  52.                     InputStream is = url.openStream(); 
  53.                     byte[] buf = new byte[1024 * 1000]; 
  54.                     int len = 0
  55.  
  56.                     while ((len = is.read(buf)) > 0) { 
  57.                         System.out.println("--------------文件大于20K,准备下载:" 
  58.                                 + picUrl); 
  59.                         fos.write(buf, 0, len); 
  60.                     } 
  61.                     System.out.println("!!!!!!!!!!!!!!!###文件大小:" + fileSize); 
  62.                     if (is != null) 
  63.                         is.close(); 
  64.                     if (fos != null) 
  65.                         fos.close(); 
  66.                 } 
  67.             } catch (Exception e) { 
  68.                 e.printStackTrace(); 
  69.             } 
  70.         } 
  71.     } 
  72.  
  73.     public static void main(String[] args) { 
  74.  
  75.         String baseUrl = "http://danhuaer.com/ooxx/comment-page-{#page}#comments"
  76.         CrawPicForDanHuaer crawPicForDanHuaer = new CrawPicForDanHuaer(); 
  77.         for (int i = 1; i < 100; i++) { 
  78.             String url = baseUrl.replace("{#page}", Integer.toString(i)); 
  79.             System.out.println("####开始下载图片的页面:" + url); 
  80.             crawPicForDanHuaer.crawImage(url, "UTF-8"); 
  81.             try { 
  82.                 Thread.sleep(1000); 
  83.             } catch (InterruptedException e) { 
  84.                 e.printStackTrace(); 
  85.             } 
  86.         } 
  87.  
  88.     } 
  89.