コード例 #1
0
 private void button1_Click(object sender, EventArgs e)
 {
     try
     {
         string          pageHtml = HttpRequestUtil.GetPageHtmlGB2312("https://cjb.alipay.com/gold/guide.htm");
         Regex           regA     = new Regex("(?:<span class=\"ft-56\">)[\\s\\S]*?</span>元/克");
         MatchCollection mc       = regA.Matches(pageHtml);
         foreach (Match match in mc)
         {
             string temp  = match.ToString().Replace("<span class=\"ft-56\">", "").Replace("</span>元/克", "");
             double price = double.Parse(temp.Trim());
         }
     }
     catch { }
 }
コード例 #2
0
        private void btn_vehicleType_Click(object sender, EventArgs e)
        {
            string VLetter;        //首字母
            string AType = "";     //
            string BType = "";     //
            string CType = "";     //
            string Price = "暂无价格"; //价格

            for (int i = 0; i < 26; i++)
            {
                DataTable dt = GetTable();
                VLetter = Encoding.ASCII.GetString(new byte[] { (byte)(i + 65) });
                //VLetter = GetUpperLetter()[i];
                string pageHtml = HttpRequestUtil.GetPageHtmlGB2312("http://www.autohome.com.cn/grade/carhtml/" + VLetter + ".html");
                //string pageHtml = HttpRequestUtil.GetPageHtmlGB2312("http://price.pcauto.com.cn/cars/");
                //Regex regTypeVehicle = new Regex("<div class=\"h3-tit\">[\\s\\S]*?</dd>"); 主型号有误
                Regex regTypeVehicle = new Regex("<div><a href=\"http://car.autohome.com.cn/pic[\\s\\S]*?</dd>");

                MatchCollection mc = regTypeVehicle.Matches(pageHtml);
                foreach (Match match in mc)
                {
                    string vChildren = match.Value.ToString();
                    //==
                    //Regex regA = new Regex("(?<=(<div class=\"h3-tit\">)).*?(?=<)");主型号有误
                    Regex regA   = new Regex("(?<=<div><a href=\"http://car.autohome.com.cn/pic/.*?>).*?(?=<)");
                    Match matchA = regA.Match(vChildren);
                    AType = matchA.Value;
                    //==
                    Regex           regTypeVChildren = new Regex("<div class=\"h3-tit\">[\\s\\S]*?</ul>");
                    MatchCollection mcChildren       = regTypeVChildren.Matches(vChildren);
                    foreach (Match citem in mcChildren)
                    {
                        string Type = citem.Value.ToString();

                        //==
                        Regex regB   = new Regex("(?<=(<div class=\"h3-tit\">)).*?(?=<)");
                        Match matchB = regB.Match(Type);
                        BType = matchB.Value;
                        //==

                        Regex regType = new Regex("(<h4 class=\"toolong\"[\\s\\S]*?</li>)|(<h4><a href=[\\s\\S]*?</li>)");
                        // Regex regType = new Regex("<h4><a href=[\\s\\S]*?</li>");
                        MatchCollection mcType = regType.Matches(Type);
                        foreach (Match tItem in mcType)
                        {
                            string          vData    = tItem.Value.ToString();
                            Regex           regVData = new Regex("<a[\\s\\S]*?</a>");
                            MatchCollection mcVData  = regVData.Matches(vData);
                            //foreach (var item in mcVData)
                            //{
                            string          vValue   = tItem.Value.ToString();
                            Regex           regValue = new Regex("\">[\\s\\S]*?</a>");
                            MatchCollection mcValue  = regVData.Matches(vData);
                            if (mcValue.Count == 4 || mcValue.Count == 3 || mcValue.Count == 5)
                            {
                                Regex regC   = new Regex("(?<=>).*?(?=<)");
                                Match matchC = regC.Match(mcValue[0].ToString());
                                CType = matchC.Value;
                                dt.Rows.Add(0, VLetter, AType, BType, CType, "暂无价格");
                            }
                            else if (mcValue.Count == 6)
                            {
                                Regex regC   = new Regex("(?<=>).*?(?=<)");
                                Match matchC = regC.Match(mcValue[0].ToString());
                                CType = matchC.Value;

                                Match matchP = regC.Match(mcValue[1].ToString());
                                Price = matchP.Value;
                                dt.Rows.Add(0, VLetter, AType, BType, CType ?? "类型错误", Price ?? "价格错误");
                            }
                            //}
                        }
                    }
                }

                try
                {
                    //写库
                    //ConnectionStringSettingsCollection connections = ConfigurationManager.ConnectionStrings;
                    //string conStr = connections["constrLocal"].ToString();
                    string conStr = "Data Source=127.0.0.1;Initial Catalog=EFDemo;User ID=sa;Password=123456";
                    SqlBuckCopyHelper.SqlBulkCopyByDatatable(conStr, "VehicleType", dt);
                    //进度
                    MessageBox.Show("车型入库完成");
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.ToString());
                }
            }
        }