给梦一个奔跑的方向!
PDF Print E-mail
User Rating: / 0
PoorBest 
Written by xlingfairy
Tuesday, 12 January 2010 15:47
bigCatReg.Matches(ctx) 就不会超时,
但是  smallCatReg.Matches(ctx); 无伦怎么写都会超时,如图所示.
不知道是什么原因.
如果不设置中断的话,最后得到的 pcs 的 count 是0.
正则表达式,我确定执行是有结果的,如图.
        private string GetUrlCtx(string url) {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(url);
            req.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7 (.NET CLR 3.5.30729) QQDownload/1.7";
            HttpWebResponse rep = (HttpWebResponse)req.GetResponse();
            StreamReader repStream = new StreamReader(rep.GetResponseStream(), Encoding.Default);
            string ctx = repStream.ReadToEnd();
            return ctx;
        }
        private void GetBigCats() {
            string ctx = GetUrlCtx(catUrl);
            MatchCollection ms = bigCatReg.Matches(ctx);
            
            //string _ctx = "";
            //MatchCollection _ms;
            foreach (Match ma in ms) {
                // 直接调用下面这个函数,不知道为什么,居然会超时!计算不出子分类!
                //GetSubCats("http://www.ttago.com/" + ma.Groups["url"].Value, ma.Groups["name"].Value);
                bigCats.Add(ma.Groups["name"].Value.Trim(), "http://www.ttago.com/" + ma.Groups["url"].Value);
            }
        }
        private void GetSubCats(string url,string catName) {
            string ctx = GetUrlCtx(url);
            MatchCollection ms = smallCatReg.Matches(ctx);// 这个居然会超时!
            foreach (Match ma in ms) {
                ProductCategory pc = new ProductCategory();
                pc.CategoryName = ma.Groups["name"].Value.Trim();
                pc.ParentCategoryName = catName;
                pc.CategoryUrl = "http://www.ttago.com/" + ma.Groups["url"].Value;
                pcs.Add(pc);
            }
        }
        public override List<ProductCategory> GetProductCategoryList() {
            GetBigCats();
            foreach( string cat in bigCats.Keys ){
                GetSubCats(bigCats[cat], cat);
            }
            return pcs;
        }

 
为什么呢???
-------------------------
真丢人啊,在写表达式的时候,后面多加了一个空格.
private readonly Regex subCatReg = new Regex("<LI><A[\\S\\s]*?href=\"(?<url>[^\"]*)\">(?<name>[\\s\\S]*?)</A></LI> ", RegexOptions.Compiled | RegexOptions.IgnoreCase);
Last Updated ( Wednesday, 13 January 2010 11:39 )
 

Add comment


Security code
Refresh

Popular Contents

Recommend

Site Info

Members : 1
Content : 100
Web Links : 7
Content View Hits : 56491

Links