免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2080 | 回复: 0
打印 上一主题 下一主题

提取百度搜索结果 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-08-20 10:45 |只看该作者 |倒序浏览
通过正则表达式提取百度搜索结果

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 static 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 static 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 static void baiduparser(String html)
      {
       Pattern pattern = Pattern.compile("([\\s\\S]+?)([\\s\\S]+?)
/**
  * 主函数
  * @param argc
  * @throws Exception
  */

public  static void  main(String[] argc)throws Exception
{
  System.err.println("Now Let's Go!");
  jiecheng.baiduparser(jiecheng.getHtml("
http://www.baidu.com/s?wd
=中国"));
  System.err.println("Good bye!");  
}
}


本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u2/80678/showart_2032579.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP