Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1677514
  • 博文数量: 210
  • 博客积分: 10013
  • 博客等级: 上将
  • 技术积分: 2322
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-25 15:56
文章分类

全部博文(210)

文章存档

2011年(34)

2010年(121)

2009年(37)

2008年(18)

我的朋友

分类: Java

2009-08-20 10:45:12

通过正则表达式提取百度搜索结果
 

package test;

import java.io.*;
import java.util.*;
import java.text.*;
import java.net.*;
import java.util.regex.*;
/**
 * 提取百度搜索结果标题链接摘要
 * @author 静止的流水
 *
 */

public class GetBaiduInfor
{

    /**
     * 下载页面源码
     * @param urlString
     * @return
     */

     public String getHtml(String urlString) {
         try {
         StringBuffer html = new StringBuffer();
         URL url = new URL(urlString);
         HttpURLConnection conn = (HttpURLConnection) url.openConnection();
         InputStreamReader isr = new InputStreamReader(conn.getInputStream());
         BufferedReader br = new BufferedReader(isr);
         String temp;
         while ((temp = br.readLine()) != null) {
         html.append(temp).append("\n");
         }
         br.close();
         isr.close();
         return html.toString();
         } catch (Exception e) {
         e.printStackTrace();
         return null;
         }
         }
     /**
     * 字符串查找
     * @param expression 正则表达式字符串
     * @param text 要进行查找操作的字符串
     * @param str 要查找的字符串
     */

     private void findText(String expression, String text, String str) {
     Pattern p = Pattern.compile(expression); // 正则表达式

     Matcher m = p.matcher(text); // 操作的字符串

     StringBuffer sb = new StringBuffer();
     int i = 0;
     while (m.find()) {
     m.appendReplacement(sb, str);
     i++;
     }
     m.appendTail(sb);
     System.out.println(sb.toString());
     System.out.println(i);
     }

     /**
     * 提取百度结果标题连接摘要
     * @param html
     */

     public void baiduparser(String html)
     {
          Pattern pattern = Pattern.compile("([\\s\\S]+?)([\\s\\S]+?));
          Matcher m = pattern.matcher(html);
         
          int i = 1;
          while(m.find())
          {
              System.out.println(i);
              //System.out.println(m.group());

              System.out.println("href = "+m.group(1));
              String chinacode = jiecheng.china(m.group(2));
              System.out.println("Title = "+chinacode);
              String chinacode3 = jiecheng.china(m.group(3));
              System.out.println("Description = "+chinacode3);
              i++;
          }
          System.out.println("m.groupCount = "+m.groupCount());
         
     }
     /**
     * 判断中文字符
     * @param s
     * @return
     */

     public String china(String s)
     {
              StringBuilder sb = new StringBuilder();
              for(int i=0;i<s.length();i++)
              {
              if(s.substring(i, i+1).matches("[\u4e00-\u9fa5]"))
              {
          sb.append(s.subSequence(i, i+1));
              }
          }
              return sb.toString();
     }

    /**
     * 主函数
     * @param argc
     * @throws Exception
     */

    
    public static void main(String[] argc)throws Exception
    {
        System.err.println("Now Let's Go!");
        GetBaiduInfor getbaiduinfor = new GetBaiduInfor();
        getbaiduinfor.baiduparser(getbaiduinfor.getHtml("中国"));
        System.err.println("Good bye!");        
    }
    }


阅读(5224) | 评论(4) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2011-04-19 21:46:07

jiecheng.china 在哪呢,发个链接吧

chinaunix网友2010-06-17 15:52:51

jiecheng.china()在哪?还没修改吧

chinaunix网友2009-09-22 14:47:55

jiecheng.china() 根本就没有这个函数

chinaunix网友2009-09-22 14:45:56

jiecheng.china是什么意思