using System;
using System.Text;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;using Boolue.Real;namespace City
{
    public class CCity
    {
        static string CurrentURL;        public CCity()
        {
            CurrentURL = "";
        }        public string GetHtml(string Url)
        {
            CurrentURL = Url;
            string Html = RealPage.Get(Url);
            if (Html.Length == 0)
            {
                throw new Exception();
            }
            return Html;
        }        public PostSection[] GetSection(string html)
        {
            ProvideRegex PR = new ProvideRegex();
            Regex ReList = new Regex(PR.SectionList);
            Match MCList = ReList.Match(html);
            string StrResults = MCList.Groups[1].Value.Trim();
            if (StrResults.Length != 0)
            {
                Regex ReItem = new Regex(PR.SectionItem);
                MatchCollection McItem = ReItem.Matches(StrResults);
                int Count = McItem.Count;
                if (Count != 0)
                {
                    PostSection[] Section = new PostSection[Count];
                    for (int i = 0; i < Count; i++)
                    {
                        Section[i] = new PostSection();
                        Match MItem = McItem[i];
                        Section[i].Option = Convert.ToInt32(Regex.Replace(MItem.Groups[1].Value.Trim(), @"[^0-9]", ""));
                        Section[i].Name = MItem.Groups[2].Value.Trim();
                    }
                    return Section;
                }
                else
                {
                    throw new Exception();
                }
            }
            else
            {
                throw new Exception();
            }
        }        public string GetSubSection(int Section)
        {
            string html = PostData(Section);
            return html;//由于Post方法发送不起作用,所以暂时返回原代码先。如果Post请求成功,那么得到请求成功后的源代码,进行分析.
        }
        private string PostData(int NameId)//由于是下拉框,所以不知道是option里面的value还是text...一个一个测试,根据areaid所以暂时先测试value
        {
            string Html = "";
            byte[] PostData = Encoding.Default.GetBytes("areaid=" + NameId + "&garden=");
            HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(CurrentURL);
            Request.Method = "POST";
            Request.ContentLength = PostData.Length;
            Request.ContentType = "multipart/form-data";
            using (Stream Sm = Request.GetRequestStream())
            {
                Sm.Write(PostData, 0, PostData.Length);
                Sm.Close();
            }
            using (Stream Sm = Request.GetResponse().GetResponseStream())
            {
                using (StreamReader Sr = new StreamReader(Sm, Encoding.Default))
                {
                    Html = Sr.ReadToEnd();
                    Sr.Close();
                    Sm.Close();
                }
            }
            return Html;
        }
    }    class ProvideRegex
    {
        public string SectionList = @"name=""areaid"">(.*?)</select>";
        public string SectionItem = @"<option.*?value=(.*?)>(.*?)</";
    }    public class SectionData
    {
        public string Name;
        public string URL;
    }    public class PostSection
    {
        public string Name;
        public int Option;
    }}调用代码:CCity CC = new CCity();
        string Html = CC.GetHtml("http://xiaoqu.koubei.com/community/browse_listing_20.html");
        PostSection[] Section = CC.GetSection(Html);
        foreach (object Obj in Section)
        {
            string Name = ((PostSection)Obj).Name;
            int NameID = ((PostSection)Obj).Option;
            Html = CC.GetSubSection(NameID);
        }

解决方案 »

  1.   


    using System; 
    using System.Text; 
    using System.IO; 
    using System.Net; 
    using System.Text.RegularExpressions; using Boolue.Real; namespace City 

        public class CCity 
        { 
            static string CurrentURL;         public CCity() 
            { 
                CurrentURL = ""; 
            }         public string GetHtml(string Url) 
            { 
                CurrentURL = Url; 
                string Html = RealPage.Get(Url); 
                if (Html.Length == 0) 
                { 
                    throw new Exception(); 
                } 
                return Html; 
            }         public PostSection[] GetSection(string html) 
            { 
                ProvideRegex PR = new ProvideRegex(); 
                Regex ReList = new Regex(PR.SectionList); 
                Match MCList = ReList.Match(html); 
                string StrResults = MCList.Groups[1].Value.Trim(); 
                if (StrResults.Length != 0) 
                { 
                    Regex ReItem = new Regex(PR.SectionItem); 
                    MatchCollection McItem = ReItem.Matches(StrResults); 
                    int Count = McItem.Count; 
                    if (Count != 0) 
                    { 
                        PostSection[] Section = new PostSection[Count]; 
                        for (int i = 0; i  < Count; i++) 
                        { 
                            Section[i] = new PostSection(); 
                            Match MItem = McItem[i]; 
                            Section[i].Option = Convert.ToInt32(Regex.Replace(MItem.Groups[1].Value.Trim(), @"[^0-9]", "")); 
                            Section[i].Name = MItem.Groups[2].Value.Trim(); 
                        } 
                        return Section; 
                    } 
                    else 
                    { 
                        throw new Exception(); 
                    } 
                } 
                else 
                { 
                    throw new Exception(); 
                } 
            }         public string GetSubSection(int Section) 
            { 
                string html = PostData(Section); 
                return html;//由于Post方法发送不起作用,所以暂时返回原代码先。如果Post请求成功,那么得到请求成功后的源代码,进行分析. 
            } 
            private string PostData(int NameId)//由于是下拉框,所以不知道是option里面的value还是text...一个一个测试,根据areaid所以暂时先测试value 
            { 
                string Html = ""; 
                byte[] PostData = Encoding.Default.GetBytes("areaid=" + NameId + "&garden="); 
                HttpWebRequest Request = (HttpWebRequest)WebRequest.Create(CurrentURL); 
                Request.Method = "POST"; 
                Request.ContentLength = PostData.Length; 
                Request.ContentType = "multipart/form-data"; 
                using (Stream Sm = Request.GetRequestStream()) 
                { 
                    Sm.Write(PostData, 0, PostData.Length); 
                    Sm.Close(); 
                } 
                using (Stream Sm = Request.GetResponse().GetResponseStream()) 
                { 
                    using (StreamReader Sr = new StreamReader(Sm, Encoding.Default)) 
                    { 
                        Html = Sr.ReadToEnd(); 
                        Sr.Close(); 
                        Sm.Close(); 
                    } 
                } 
                return Html; 
            } 
        }     class ProvideRegex 
        { 
            public string SectionList = @"name=""areaid""> (.*?) </select> "; 
            public string SectionItem = @" <option.*?value=(.*?)> (.*?) </"; 
        }     public class SectionData 
        { 
            public string Name; 
            public string URL; 
        }     public class PostSection 
        { 
            public string Name; 
            public int Option; 
        } } 调用代码: CCity CC = new CCity(); 
            string Html = CC.GetHtml("http://xiaoqu.koubei.com/community/browse_listing_20.html"); 
            PostSection[] Section = CC.GetSection(Html); 
            foreach (object Obj in Section) 
            { 
                string Name = ((PostSection)Obj).Name; 
                int NameID = ((PostSection)Obj).Option; 
                Html = CC.GetSubSection(NameID); 
            }-----------------这样看的不累