码迷,mamicode.com
首页 > Windows程序 > 详细

C# 百度百科查找信息

时间:2015-08-21 15:02:09      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:

using HtmlAgilityPack;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace ConsoleApplication4
{
  /// <summary>
  /// 
  ///娱乐百度百科
  ///风之痕 20150821
  /// </summary>
    class Program
    {
       
        public static void Main(string[] args)
        {

            while (true) {
                Console.WriteLine("欢迎来到TOP小百科,请输入您的问题:");
                string key= Console.ReadLine();
                ASK(key);

            }
            Console.ReadKey();
        }
        private static void ASK(string key) {
            try
            {
                string uri = "http://baike.baidu.com/search/word?word=" + key;



                HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(uri);



                HttpWReq.Timeout = 10000;

                HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();

                StreamReader sr = new StreamReader(HttpWResp.GetResponseStream(), System.Text.Encoding.GetEncoding("UTF-8"));
                HtmlDocument doc = new HtmlDocument();
                doc.Load(sr);
                var kwBox = doc.DocumentNode.SelectSingleNode("//div[@class=‘para‘]");
                Console.WriteLine(kwBox.InnerText);
                Console.WriteLine("");
                sr.Close();
                sr.Dispose();
                HttpWResp.Close();
            }
            catch {
                Console.WriteLine("抱歉您的问题没有答案");
                Console.WriteLine("");
            }
            
        }
    
    }
}

 

C# 百度百科查找信息

标签:

原文地址:http://www.cnblogs.com/nnnnn/p/4747670.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!