用Regex抓取網頁中的ul 和 li標籤中最終的值!

來源:互聯網
上載者:User

標籤:style   blog   http   color   get   width   

            擷取你要抓取的頁面

            const string URL = "http://www.hn3ddf.gov.cn/price/GetList.html?pageno=1";
            string htmlStr = null;
            for (int i = 0; i < 10; i++)
            {
                try
                {
                    System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(URL);
                    request.Headers.Set("Pragma", "no-cache");
                    request.Timeout = 10000 + (i * 5000);
                    System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                    System.IO.Stream streamReceive = response.GetResponseStream();
                    System.IO.StreamReader streamReader = new System.IO.StreamReader(streamReceive, Encoding.GetEncoding("utf-8"));
                    htmlStr = streamReader.ReadToEnd();
                    break;
                }
                catch (Exception e)
                {
                    //----------------抓取異常!!
                }
            }

//抓取頁面中的ul 標籤中的特定一行屬性

           MatchCollection priceList = Regex.Matches(htmlStr, @"<ul style=""font-size:12px;width:320px; margin:0; padding:0;"">(.*?)</ul>", RegexOptions.Singleline);            StringBuilder resultStr = new StringBuilder();
            for (int i = 0; i < priceList.Count; i++)
            {
                try
                {
                      //<ul style="font-size:12px;width:320px; margin:0; padding:0;">
                      //  <li style="color:#555555; float:left; display:block; width:140px; height:22px; line-height:22px;" align="center">錏嬮浮閰嶅悎楗叉枡</li>
                      //  <li align="center" style="color:#555555; float:left; display:block; width:100px; height:22px; line-height:22px;">2.83鍏?鍗冨厠</li>
                      //  <li style="color:#555555; float:left; display:block; width:50px;text-align:center; height:22px; line-height:22px;">05-21</li>
                      //</ul>

                    //List<string> list = new List<string>();   //放結果的泛型集合
                    //string splitStr = "</li>";
                    //string[] strArray = priceList[i].Value.Split(splitStr.ToArray());    //一組一組的li標籤
                    //foreach (string item in strArray)
                    //{
                    //    int first = item.IndexOf(‘>‘);
                    //    int last = item.IndexOf("</li>");
                    //    list.Add(item.Substring(first, last - first));
                    //    //list.add(item.substring(item.indexof(">")));
                    //}
                    //MatchCollection items = Regex.Matches(htmlStr, @"<li.*(?=>)(.|\n)*?</li>");

                    resultStr.Append("<tr>");

                     //<li style="color:#555555; float:left; display:block; width:140px; height:22px; line-height:22px;" align="center">蛋雞配合飼料</li>

                    //<ul style="font-size:12px;width:320px; margin:0; padding:0;">
                    //    <li style="color:#555555; float:left; display:block; width:140px; height:22px; line-height:22px;" align="center">蛋雞配合飼料</li>
                    //    <li align="center" style="color:#555555; float:left; display:block; width:100px; height:22px; line-height:22px;">2.83元/千克</li>
                    //    <li style="color:#555555; float:left; display:block; width:50px;text-align:center; height:22px; line-height:22px;">05-21</li>
                    //</ul>
                    string priceItem = priceList[i].Value;
                    //string name = Regex.Match(priceItem, @"<li style=""color:#555555; float:left; display:block; width:140px; height:22px; line-height:22px;"" align=""center"">(.*?)</li>").Value;//配備<開頭的在抓取的網頁中的li標籤中的所有屬性進行配備為真的一行結果包含:樣式和值
                    Match TitleMatch = Regex.Match(priceItem, @"<li style=""color:#555555; float:left; display:block; width:140px; height:22px; line-height:22px;"" align=""center"">([^<]*)</li>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
       //取上面一行中的只有屬性的值Value.Groups[1],1 代表Regex.Match方法得到的Groups的索引是從1開始的,而不是從0開始的
                    string name = TitleMatch.Groups[1].Value;

                    //"color:#555555; float:left; display:block; width:140px; height:22px; line-height:22px;" align="center">錏嬮浮閰嶅悎楗叉枡
                    //name = name.Substring(10, name.Length - 15);
                    //name = name.Substring(113, name.Length - 118);

                    //string price = Regex.Match(priceItem, @"<li align=""center"" style=""color:#555555; float:left; display:block; width:100px; height:22px; line-height:22px;"">(.*?)</li>").Value;
                    //price = price.Substring(13, price.Length - 18);
                    //price = price.Substring(115, price.Length -120);
                    Match priceMatch = Regex.Match(priceItem, @"<li align=""center"" style=""color:#555555; float:left; display:block; width:100px; height:22px; line-height:22px;"">([^<]*)</li>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    string price = priceMatch.Groups[1].Value;
//                    string weeks = Regex.Match(priceItem, @"<li style=""color:#555555; float:left; display:block; width:50px;text-align:center; height:22px; line-height:22px;"">(.*?)</li>
//").Value;
//                    //weeks = weeks.Substring(9, weeks.Length - 16);
//                    weeks = weeks.Substring(116, weeks.Length - 122);

                    Match weeksMatch = Regex.Match(priceItem, @"<li style=""color:#555555; float:left; display:block; width:50px;text-align:center; height:22px; line-height:22px;"">([^<]*)</li>", RegexOptions.IgnoreCase | RegexOptions.Multiline);
                    string weeks = weeksMatch.Groups[1].Value;
                    resultStr.Append("<td width=\"195\" height=\"25\" align=\"left\">" + name + "</td><td width=\"70\" height=\"25\" align=\"center\" style=\"text-align:right;\">" + price + "</td><td height=\"25\" align=\"center\" style=\"color:#55a8ea;\">" + weeks + "</td>");
                    resultStr.Append("</tr>");
                    #region 原來的
                    //resultStr.Append("<tr>");
                    //string priceItem = priceList[i].Value;
                    //string name = Regex.Match(priceItem, "width=125>.*?</td>").Value;
                    //name = name.Substring(10, name.Length - 15);
                    //string price = Regex.Match(priceItem, "<td width=50.*?</td>").Value;
                    //price = price.Substring(13, price.Length - 18);
                    //string weeks = Regex.Match(priceItem, "class=en>.*?</font>").Value;
                    //weeks = weeks.Substring(9, weeks.Length - 16);
                    //resultStr.Append("<td width=\"195\" height=\"25\" align=\"left\">" + name + "</td><td width=\"70\" height=\"25\" align=\"center\">" + price + "</td><td height=\"25\" align=\"center\" style=\"color:#55a8ea;\">" + weeks + "</td>");
                    //resultStr.Append("</tr>");
                    #endregion
                }
                catch (Exception ex)
                {
                    //Common.Log4netUtil.Log().Error("擷取跨域資料錯誤." + ex.Message);
                }
            }

            return resultStr.ToString();  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.