A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 千年 中级黑马   /  2012-12-13 12:39  /  1796 人查看  /  2 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

求帮忙写一个百度收录查询的程序,
打开百度 www.baidu.com   
输入 site:www.google.com
找到相关结果数4,200,000个
把4,200,000 提出来并显示。

评分

参与人数 1技术分 +1 收起 理由
宋天琪 + 1

查看全部评分

2 个回复

倒序浏览
百度收录真快,一分钟就收录 百度一下   c#百度收录  会发现我这个页面排第一   {:soso_e113:}

评分

参与人数 1黑马币 +3 收起 理由
马毅 + 3 赞一个!

查看全部评分

回复 使用道具 举报
本帖最后由 pm324 于 2013-8-19 18:28 编辑

这里会用到
using System.Text.RegularExpressions;
using System.Net;
using System.IO;
这三个命名空间

首先我们知道百度搜索的语法是 http://www.baidu.com/s?wd=
wd后面跟的便是我们要搜索的关键词,(关于百度搜索url语法的具体用法有兴趣的同学可以M我索取)

这样我们就得到了我们要请求的页面的URL

然后用WebRequest请求指定url的的WebResponse对象,然后用StreamReader读取WebResponse中的数据流,这就获得了网页的源代码

然后呢 我们用正则进行匹配,通过观察不难看出规则

我们要取得的结果放在标签 <p class="site_tip"><strong> 和 </strong>内

那么就可以写出正则表达式  <p class=""site_tip""><strong>(.|\n)*?</strong>   来取出我们要的结果

取出来之后是带有html标签的,在这里可以有很多方法去除标签 我就不一一详说了

我用的是直接取出这个信息中的数字

具体代码在下面,不理解的同学可以找我交流
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. using System.Text.RegularExpressions;
  5. using System.Net;
  6. using System.IO;

  7. namespace 查询收录
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             Console.WriteLine("请输入您要查询的域名");
  14.             Console.WriteLine("找到相关结果数:"+collect("http://www.baidu.com/s?wd=site:" + Console.ReadLine()));
  15.             Console.ReadKey();
  16.         }
  17.         public static string collect(string url)
  18.         {
  19.             Match ma = null;
  20.             try
  21.             {
  22.                 Regex r;
  23.                 r = new Regex(@"<p class=""site_tip""><strong>(.|\n)*?</strong>");
  24.                 ma = r.Match(GetHttpData(url));
  25.                 r = new Regex(@"\d+");
  26.                 ma = r.Match(ma.ToString().Replace(",", ""));
  27.             }
  28.             catch (Exception e)
  29.             {
  30.                 Console.WriteLine(e.Message);
  31.             }
  32.             return ma.ToString();
  33.         }


  34.         public static string GetHttpData(string Url)
  35.         {

  36.             string sException = null;

  37.             string sRslt = null;

  38.             WebResponse oWebRps = null;

  39.             WebRequest oWebRqst = WebRequest.Create(Url);

  40.             oWebRqst.Timeout = 50000;

  41.             try
  42.             {

  43.                 oWebRps = oWebRqst.GetResponse();

  44.             }

  45.             catch (Exception e)
  46.             {

  47.                 sException = e.ToString();

  48.                 Console.WriteLine(sException);

  49.             }

  50.             finally
  51.             {

  52.                 if (oWebRps != null)
  53.                 {
  54.                     StreamReader oStreamRd = new StreamReader(oWebRps.GetResponseStream(), Encoding.GetEncoding("UTF-8"));

  55.                     sRslt = oStreamRd.ReadToEnd();

  56.                     oStreamRd.Close();

  57.                     oWebRps.Close();

  58.                 }

  59.             }

  60.             return sRslt;

  61.         }
  62.     }
  63. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马