现在的位置: 首页 > 综合 > 正文

Java读取网页数据并分析

2013年09月16日 ⁄ 综合 ⁄ 共 4754字 ⁄ 字号 评论关闭


  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.  public String getOneHtml(final String htmlurl) throws IOException   
  17.  {   
  18.   URL url;   
  19.   String temp;   
  20.   final StringBuffer sb = new StringBuffer();   
  21.   try  
  22.   {   
  23.    url = new URL(htmlurl);   
  24.    final BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream(), "utf-8"));// 读取网页全部内容   
  25.    while ((temp = in.readLine()) != null)   
  26.    {   
  27.     sb.append(temp);   
  28.    }   
  29.    in.close();   
  30.   }   
  31.   catch (final MalformedURLException me)   
  32.   {   
  33.    System.out.println("你输入的URL格式有问题!请仔细输入");   
  34.    me.getMessage();   
  35.    throw me;   
  36.   }   
  37.   catch (final IOException e)   
  38.   {   
  39.    e.printStackTrace();   
  40.    throw e;   
  41.   }   
  42.   return sb.toString();   
  43.  }   
  44.   
  45.    
  46.  public String getTitle(final String s)   
  47.  {   
  48.   String regex;   
  49.   String title = "";   
  50.   final List<String> list = new ArrayList<String>();   
  51.   regex = "<title>.*?</title>";   
  52.   final Pattern pa = Pattern.compile(regex, Pattern.CANON_EQ);   
  53.   final Matcher ma = pa.matcher(s);   
  54.   while (ma.find())   
  55.   {   
  56.    list.add(ma.group());   
  57.   }   
  58.   for (int i = 0; i < list.size(); i++)   
  59.   {   
  60.    title = title + list.get(i);   
  61.   }   
  62.   return outTag(title);   
  63.  }   
  64.   
  65.    
  66.  public List<String> getLink(final String s)   
  67.  {   
  68.   String regex;   
  69.   final List<String> list = new ArrayList<String>();   
  70.   regex = "<a[^>]*href=(\"([^\"]*)\"|\'([^\']*)\'|([^\\s>]*))[^>]*>(.*?)</a>";   
  71.   final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);   
  72.   final Matcher ma = pa.matcher(s);   
  73.   while (ma.find())   
  74.   {   
  75.    list.add(ma.group());   
  76.   }   
  77.   return list;   
  78.  }   
  79.   
  80.    
  81.  public List<String> getScript(final String s)   
  82.  {   
  83.   String regex;   
  84.   final List<String> list = new ArrayList<String>();   
  85.   regex = "<script.*?</script>";   
  86.   final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);   
  87.   final Matcher ma = pa.matcher(s);   
  88.   while (ma.find())   
  89.   {   
  90.    list.add(ma.group());   
  91.   }   
  92.   return list;   
  93.  }   
  94.   
  95.    
  96.  public List<String> getCSS(final String s)   
  97.  {   
  98.   String regex;   
  99.   final List<String> list = new ArrayList<String>();   
  100.   regex = "<style.*?</style>";   
  101.   final Pattern pa = Pattern.compile(regex, Pattern.DOTALL);   
  102.   final Matcher ma = pa.matcher(s);   
  103.   while (ma.find())   
  104.   {   
  105.    list.add(ma.group());   
  106.   }   
  107.   return list;   
  108.  }   
  109.   
  110.    
  111.  public String outTag(final String s)   
  112.  {   
  113.   return s.replaceAll("<.*?>""");   
  114.  }   
  115.   
  116.    
  117.  public HashMap<String, String> getFromYahoo(final String s)   
  118.  {   
  119.   final HashMap<String, String> hm = new HashMap<String, String>();   
  120.   final StringBuffer sb = new StringBuffer();   
  121.   String html = "";   
  122.   System.out.println("\n------------------开始读取网页(" + s + ")--------------------");   
  123.   try  
  124.   {   
  125.    html = getOneHtml(s);   
  126.   }   
  127.   catch (final Exception e)   
  128.   {   
  129.    e.getMessage();   
  130.   }   
  131.   // System.out.println(html);   
  132.   System.out.println("------------------读取网页(" + s + ")结束--------------------\n");   
  133.   System.out.println("------------------分析(" + s + ")结果如下--------------------\n");   
  134.   String title = outTag(getTitle(html));   
  135.   title = title.replaceAll("_雅虎知识堂""");   
  136.   // Pattern pa=Pattern.compile("<div   
  137.   // class=\"original\">(.*?)((\r\n)*)(.*?)((\r\n)*)(.*?)</div>",Pattern.DOTALL);   
  138.   final Pattern pa = Pattern.compile("<div class=\"original\">(.*?)</p></div>", Pattern.DOTALL);   
  139.   final Matcher ma = pa.matcher(html);   
  140.   while (ma.find())   
  141.   {   
  142.    sb.append(ma.group());   
  143.   }   
  144.   String temp = sb.toString();   
  145.   temp = temp.replaceAll("(<br>)+?""\n");// 转化换行   
  146.   temp = temp.replaceAll("<p><em>.*?</em></p>""");// 去图片注释   
  147.   hm.put("title", title);   
  148.   hm.put("original", outTag(temp));   
  149.   return hm;   
  150.   
  151.  }   
  152.   
  153.    
  154.  public static void main(final String args[])   
  155.  {   
  156.   String url = "";   
  157.   final List<String> list = new ArrayList<String>();   
  158.   System.out.print("输入URL,一行一个,输入结束后输入 go 程序开始运行:   \n");   
  159.     
  160.   final BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
  161.   try  
  162.   {   
  163.    while (!(url = br.readLine()).equals("go"))   
  164.    {   
  165.     list.add(url);   
  166.    }   
  167.   }   
  168.   catch (final Exception e)   
  169.   {   
  170.    e.getMessage();   
  171.   }   
  172.   final WebContent wc = new WebContent();   
  173.   HashMap<String, String> hm = new HashMap<String, String>();   
  174.   for (int i = 0; i < list.size(); i++)   
  175.   {   
  176.    hm = wc.getFromYahoo(list.get(i));   
  177.    System.out.println("标题: " + hm.get("title"));   
  178.    System.out.println("内容: \n" + hm.get("original"));   
  179.   }   
  180.     
  181.     
  182.   
  183.     
  184.   
  185.   // System.out.println(htmlurl+"网页内容结束");   
  186.     
  187.  }   

抱歉!评论已关闭.