System.Net.WebClient
获取数据,再用正则分析数据,获取指定内容

解决方案 »

  1.   

    ///获取网页源文件
    private string GetHtmlSource(string url)
            {
                string html = "";
                try
                {
                    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
                    
                    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                    Stream stream = response.GetResponseStream();
                    StreamReader reader = new StreamReader(stream, Encoding.GetEncoding("GB2312"));
                    html = reader.ReadToEnd();
                }
                catch
                {
                    MessageBox.Show("无法连接到远程服务器,请检查您的网络是否正常!", "提示");
                }
                return html;                   }
            #endregion
      

  2.   


    http://www.cnblogs.com/skyiv/archive/2005/10/01/GetIP.html这里有“获取网页的HTML内容”的代码。
      

  3.   

    我想多给几个链接,让大家参考一下:
    http://www.whitepages.com/5116/search/FindPerson?firstname=&name=wa&name_begins_with=1&city_zip=+01002&state_id=All+US&x=53&y=16
      

  4.   

    try
    //string src = GetHtmlSource("http://www.whitepages.com/5116/search/Replay?search_id=40051390946113532971");
    string src = GetHtmlSource("http://www.whitepages.com/5116/search/FindPerson?firstname=&name=wa&name_begins_with=1&city_zip=+01002&state_id=All+US&x=53&y=16");
    MatchCollection mc = Regex.Matches(src, @"<div\s+class=""description"">\s*<h2><a[^>]*>(?<name>[\s\S]*?)</a>\s*</h2>\s*<p>(?<adress>[\s\S]*?)</p>\s*<p>(?<code>[\s\S]*?)</p>\s*<p[^>]*>(?<phone>[\s\S]*?)</p>", RegexOptions.IgnoreCase);
    foreach (Match m in mc)
    {
        richTextBox2.Text += m.Groups["name"].Value + "\n";
        richTextBox2.Text += m.Groups["adress"].Value + "\n";
        richTextBox2.Text += m.Groups["code"].Value + "\n";
        richTextBox2.Text += m.Groups["phone"].Value + "\n\n";
    }private string GetHtmlSource(string url)
    {
        System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(url);
        request.UserAgent = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.2; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1) Web-Sniffer/1.0.24";
        System.Net.WebResponse response = request.GetResponse();
        System.IO.Stream resStream = response.GetResponseStream();
        System.IO.StreamReader sr = new System.IO.StreamReader(resStream, System.Text.Encoding.UTF8);
        string htmlSource = (sr.ReadToEnd());
        resStream.Close();
        sr.Close();
        return htmlSource;         
    }
      

  5.   

    谢谢,lxcnn(过客) ;问题已解决......
      

  6.   

    谢谢,lxcnn(过客) ;问题已解决......