`
likenice
  • 浏览: 63041 次
  • 来自: 北京
社区版块
存档分类
最新评论

简易版web页面分析采集器

阅读更多
  1. import java.io.BufferedReader;   
  2. import java.io.IOException;   
  3. import java.io.InputStreamReader;   
  4. import java.net.MalformedURLException;   
  5. import java.net.URL;   
  6. import java.util.ArrayList;   
  7. import java.util.HashMap;   
  8. import java.util.List;   
  9. import java.util.regex.Matcher;   
  10. import java.util.regex.Pattern;   
  11.   
  12.   
  13. public class WebContent   
  14. {   
  15.  /**  
  16.   * 读取一个网页全部内容  
  17.   */  
  18.  public String getOneHtml(final String htmlurl) throws IOException   
  19.  {   
  20.   URL url;   
  21.   String temp;   
  22.   final StringBuffer sb = new StringBuffer();   
  23.   try  
  24.   {   
  25.    url = new URL(htmlurl);   
  26.    final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));// 读取网页全部内容   
  27.    while ((temp = in.readLine()) != null)   
  28.    {   
  29.     sb.append(temp);   
  30.    }   
  31.    in.close();   
  32.   }   
  33.   catch (final MalformedURLException me)   
  34.   {   
  35.    System.out.println("你输入的URL格式有问题!请仔细输入");   
  36.    me.getMessage();   
  37.    throw me;   
  38.   }   
  39.   catch (final IOException e)   
  40.   {   
  41.    e.printStackTrace();   
  42.    throw e;   
  43.   }   
  44.   return sb.toString();   
  45.  }   
  46.   
  47.  /**  
  48.   *   
  49.   * @param s  
  50.   * @return 获得网页标题  
  51.   */  
  52.  public String getTitle(final String s)   
  53.  {   
  54.   String regex;   
  55.   String title = "";   
  56.   final List<String> list = new ArrayList<String>();   
  57.   regex = "<title>.*?</title>";   
  58.   final Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);   
  59.   final Matcher ma = pa.matcher(s);   
  60.   while (ma.find())   
  61.   {   
  62.    list.add(ma.group());   
  63.   }   
  64.   for (int i = 0; i < list.size(); i++)   
  65.   {   
  66.    title = title + list.get(i);   
  67.   }   
  68.   return outTag(title);   
  69.  }   
  70.   
  71.  /**  
  72.   *   
  73.   * @param s  
  74.   * @return 获得链接  
  75.   */  
  76.  public List<String> getLink(final String s)   
  77.  {   
  78.   String regex;   
  79.   final List<String> list = new ArrayList<String>();   
  80.   regex = "<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>";   
  81.   final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);   
  82.   final Matcher ma = pa.matcher(s);   
  83.   while (ma.find())   
  84.   {   
  85.    list.add(ma.group());   
  86.   }   
  87.   return list;   
  88.  }   
  89.   
  90.  /**  
  91.   *   
  92.   * @param s  
  93.   * @return 获得脚本代码  
  94.   */  
  95.  public List<String> getScript(final String s)   
  96.  {   
  97.   String regex;   
  98.   final List<String> list = new ArrayList<String>();   
  99.   regex = "<script.*?</script>";   
  100.   final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);   
  101.   final Matcher ma = pa.matcher(s);   
  102.   while (ma.find())   
  103.   {   
  104.    list.add(ma.group());   
  105.   }   
  106.   return list;   
  107.  }   
  108.   
  109.  /**  
  110.   *   
  111.   * @param s  
  112.   * @return 获得CSS  
  113.   */  
  114.  public List<String> getCSS(final String s)   
  115.  {   
  116.   String regex;   
  117.   final List<String> list = new ArrayList<String>();   
  118.   regex = "<style.*?</style>";   
  119.   final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);   
  120.   final Matcher ma = pa.matcher(s);   
  121.   while (ma.find())   
  122.   {   
  123.    list.add(ma.group());   
  124.   }   
  125.   return list;   
  126.  }   
  127.   
  128.  /**  
  129.   *   
  130.   * @param s  
  131.   * @return 去掉标记  
  132.   */  
  133.  public String outTag(final String s)   
  134.  {   
  135.   return s.replaceAll("<.*?>""");   
  136.  }   
  137.   
  138.  /**  
  139.   *   
  140.   * @param s  
  141.   * @return 获取雅虎知识堂文章标题及内容  
  142.   */  
  143.  public HashMap<String, String> getFromYahoo(final String s)   
  144.  {   
  145.   final HashMap<String, String> hm = new HashMap<String, String>();   
  146.   final StringBuffer sb = new StringBuffer();   
  147.   String html = "";   
  148.   System.out.println("\n------------------开始读取网页(" + s + ")--------------------");   
  149.   try  
  150.   {   
  151.    html = getOneHtml(s);   
  152.   }   
  153.   catch (final Exception e)   
  154.   {   
  155.    e.getMessage();   
  156.   }   
  157.   // System.out.println(html);   
  158.   System.out.println("------------------读取网页(" + s + ")结束--------------------\n");   
  159.   System.out.println("------------------分析(" + s + ")结果如下--------------------\n");   
  160.   String title = outTag(getTitle(html));   
  161.   title = title.replaceAll("_雅虎知识堂""");   
  162.   // Pattern pa=Pattern.compile("<div   
  163.   // class=\"original\">(.*?)((\r\n)*)(.*?)((\r\n)*)(.*?)</div>",Pattern.DOTALL);   
  164.   final Pattern pa = Pattern.compile("<div class=\"original\">(.*?)</p></div>", Pattern.DOTALL);   
  165.   final Matcher ma = pa.matcher(html);   
  166.   while (ma.find())   
  167.   {   
  168.    sb.append(ma.group());   
  169.   }   
  170.   String temp = sb.toString();   
  171.   temp = temp.replaceAll("(<br>)+?""\n");// 转化换行   
  172.   temp = temp.replaceAll("<p><em>.*?</em></p>""");// 去图片注释   
  173.   hm.put("title", title);   
  174.   hm.put("original", outTag(temp));   
  175.   return hm;   
  176.   
  177.  }   
  178.   
  179.  /**  
  180.   *   
  181.   * @param args  
  182.   *            测试一组网页,针对雅虎知识堂  
  183.   */  
  184.  public static void main(final String args[])   
  185.  {   
  186.   String url = "";   
  187.   final List<String> list = new ArrayList<String>();   
  188.   System.out.print("输入URL,一行一个,输入结束后输入 go 程序开始运行:   \n");   
  189.   /*  
  190.    * http://ks.cn.yahoo.com/question/1307121201133.html  
  191.    * http://ks.cn.yahoo.com/question/1307121101907.html  
  192.    * http://ks.cn.yahoo.com/question/1307121101907_2.html  
  193.    * http://ks.cn.yahoo.com/question/1307121101907_3.html  
  194.    * http://ks.cn.yahoo.com/question/1307121101907_4.html  
  195.    * http://ks.cn.yahoo.com/question/1307121101907_5.html  
  196.    * http://ks.cn.yahoo.com/question/1307121101907_6.html  
  197.    * http://ks.cn.yahoo.com/question/1307121101907_7.html  
  198.    * http://ks.cn.yahoo.com/question/1307121101907_8.html  
  199.    */  
  200.   final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
  201.   try  
  202.   {   
  203.    while (!(url = br.readLine()).equals("go"))   
  204.    {   
  205.     list.add(url);   
  206.    }   
  207.   }   
  208.   catch (final Exception e)   
  209.   {   
  210.    e.getMessage();   
  211.   }   
  212.   final WebContent wc = new WebContent();   
  213.   HashMap<String, String> hm = new HashMap<String, String>();   
  214.   for (int i = 0; i < list.size(); i++)   
  215.   {   
  216.    hm = wc.getFromYahoo(list.get(i));   
  217.    System.out.println("标题: " + hm.get("title"));   
  218.    System.out.println("内容: \n" + hm.get("original"));   
  219.   }   
  220.   /*  
  221.    * String htmlurl[] = {  
  222.    * "http://ks.cn.yahoo.com/question/1307121201133.html",  
  223.    * "http://ks.cn.yahoo.com/question/1307121101907.html",  
  224.    * "http://ks.cn.yahoo.com/question/1307121101907_2.html",  
  225.    * "http://ks.cn.yahoo.com/question/1307121101907_3.html",  
  226.    * "http://ks.cn.yahoo.com/question/1307121101907_4.html",  
  227.    * "http://ks.cn.yahoo.com/question/1307121101907_5.html",  
  228.    * "http://ks.cn.yahoo.com/question/1307121101907_6.html",  
  229.    * "http://ks.cn.yahoo.com/question/1307121101907_7.html",  
  230.    * "http://ks.cn.yahoo.com/question/1307121101907_8.html" }; WebContent  
  231.    * wc = new WebContent(); HashMap<String, String> hm = new HashMap<String,  
  232.    * String>(); for (int i = 0; i < htmlurl.length; i++) { hm =  
  233.    * wc.getFromYahoo(htmlurl[i]); System.out.println("标题: " +  
  234.    * hm.get("title")); System.out.println("内容: \n" + hm.get("original")); }  
  235.    */  
  236.   /*  
  237.    * String html=""; String link=""; String sscript=""; String content="";  
  238.    * System.out.println(htmlurl+" 开始读取网页内容:");  
  239.    * html=wc.getOneHtml(htmlurl); System.out.println(htmlurl+"  
  240.    * 读取完毕开始分析……"); html=html.replaceAll("(<script.*?)((\r\n)*)(.*?)((\r\n)*)(.*?)(</script>)","  
  241.    * ");//去除脚本 html=html.replaceAll("(<style.*?)((\r\n)*)(.*?)((\r\n)*)(.*?)(</style>)","  
  242.    * ");//去掉CSS html=html.replaceAll("<title>.*?</title>"," ");//除去页面标题  
  243.    * html=html.replaceAll("<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>","  
  244.    * ");//去掉链接 html=html.replaceAll("(\\s){2,}?"," ");//除去多余空格  
  245.    * html=wc.outTag(html);//多余标记 System.out.println(html);  
  246.    */  
  247.   
  248.   /*  
  249.    * String s[]=html.split(" +"); for(int i=0;i<s.length;i++){  
  250.    * content=(content.length()>s[i].length())?content:s[i]; }  
  251.    * System.out.println(content);  
  252.    */  
  253.   
  254.   // System.out.println(htmlurl+"网页内容结束");   
  255.   /*  
  256.    * System.out.println(htmlurl+"网页脚本开始:"); List  
  257.    * script=wc.getScript(html); for(int i=0;i<script.size();i++){  
  258.    * System.out.println(script.get(i)); }  
  259.    * System.out.println(htmlurl+"网页脚本结束:");  
  260.    *   
  261.    * System.out.println(htmlurl+"CSS开始:"); List css=wc.getCSS(html);  
  262.    * for(int i=0;i<css.size();i++){ System.out.println(css.get(i)); }  
  263.    * System.out.println(htmlurl+"CSS结束:");  
  264.    *   
  265.    * System.out.println(htmlurl+"全部链接内容开始:"); List list=wc.getLink(html);  
  266.    * for(int i=0;i<list.size();i++){ link=list.get(i).toString(); }  
  267.    * System.out.println(htmlurl+"全部链接内容结束:");  
  268.    *   
  269.    * System.out.println("内容"); System.out.println(wc.outTag(html));  
  270.    */  
  271.  }   
  272. }  
分享到:
评论

相关推荐

    基于HTML5+css3+JavaScript+java+mysql 实现的管理系统.zip

    【CSS3】CSS3是层叠样式表的最新版本,提供了更丰富的选择器、动画和过渡效果。例如,它可以实现多列布局、媒体查询(Media Queries)以实现响应式设计,以及阴影、渐变、圆角等视觉效果。CSS3还引入了Flexbox和Grid...

    学校实训JSP项目-[新闻文章]自动新闻采集系统.zip

    【新闻文章】自动新闻采集系统是一个基于JSP技术的学校实训项目,主要目的是让学生了解和实践Web开发中的内容管理系统(CMS)设计与实现。这个系统能够自动化地从多个新闻源抓取并展示新闻,帮助用户获取实时资讯。...

    .net开源的综合开发框架wojilu框架.zip

    wojilu Log一个轻量级 Json 解析器一个简易的前端 Ajax 库(弹窗、局部刷新、验证、上传等)2. 我记录网站综合系统 2.0名称: 我记录网站综合系统 2.0网址: http://www.wojilu.com下载: 请到论坛置顶帖子中下载。...

    IWMS网站管理系统下载

    ·集成讨论区,可作为留言版或简易论坛 ·首页、 分类、新闻、专题可生成静态页面 ·生成速度可在每分钟30页到2000页之间选择 ·首页静态文件自动定时生成 ·新闻采集功能,采集时可进行其它管理操作,采集规则...

    iwms网站管理系统免费风格模板

    ·集成讨论区,可作为留言版或简易论坛 ·首页、 分类、新闻、专题可生成静态页面 ·生成速度可在每分钟30页到2000页之间选择 ·首页静态文件自动定时生成 ·新闻采集功能,采集时可进行其它管理操作,采集规则...

    DotNetTextBox所见即所得编辑器控件 v3.3.1

    &lt;br&gt;2007/7/04 Version 3.1.9 beta &lt;br&gt;Updates: 1) 增强页面信息采集功能的链接分析能力,当采集图片或超链接的时候会自动将相对路径转化为真实的网络路径,并且修正了采集功能的一些已知BUG。...

    网站管理系统整站下载

    ·集成讨论区,可作为留言版或简易论坛 ·首页、 分类、新闻、专题可生成静态页面 ·生成速度可在每分钟30页到2000页之间选择 ·首页静态文件自动定时生成 ·新闻采集功能,采集时可进行其它管理操作,采集规则...

    好用的iwms网站管理系统

    ·集成讨论区,可作为留言版或简易论坛 ·首页、 分类、新闻、专题可生成静态页面 ·生成速度可在每分钟30页到2000页之间选择 ·首页静态文件自动定时生成 ·新闻采集功能,采集时可进行其它管理操作,采集规则...

Global site tag (gtag.js) - Google Analytics