private void button1_Click(object sender, EventArgs e) { try { Task.Factory.StartNew(() => { using (SqlDbContext db = new SqlDbContext()) { QueryList = db.Database.SqlQuery <Engine>("SELECT id ,fadongjixinghao_d FROM [Web_ssp].[dbo].[Web_gonggao] where DATALENGTH(fadongjixinghao_d) > 1 and title not like '%摩托车%' and title not like '%电动%'").ToList(); //var fadongji = db.Database.SqlQuery<fadongji>("select EngineName,MaxHorsepower from ").ToList(); for (int i = 12165; i < QueryList.Count; i++) { string[] fdjxh = Regex.Split(QueryList[i].fadongjixinghao_d, "<br />", RegexOptions.IgnoreCase); string updata = string.Empty; if (fdjxh.Length > 1) { for (int q = 0; q < fdjxh.Length; q++) { if (q == fdjxh.Length - 1) { updata += db.Database.SqlQuery <string>("select MaxHorsepower from Web_engine where EngineName='" + fdjxh[q] + "'").SingleOrDefault(); } else { updata += db.Database.SqlQuery <string>("select MaxHorsepower from Web_engine where EngineName='" + fdjxh[q] + "'").SingleOrDefault() + "<br />"; } } } else { updata = db.Database.SqlQuery <string>("select MaxHorsepower from Web_engine where EngineName='" + fdjxh[0] + "'").SingleOrDefault(); } if (string.IsNullOrEmpty(updata)) { updata = ""; } string ret = updata.Replace("马力", ""); db.Database.ExecuteSqlCommand("update Web_gonggao set mali_d='" + ret + "' where id='" + QueryList[i].Id + "'"); } //for (int i = 0; i < QueryList.Count; i++) //{ // string[] fdjxh = Regex.Split(QueryList[i].fadongjixinghao_d, "<br />", RegexOptions.IgnoreCase); // strList.AddRange(fdjxh); //} //List<string> listString = new List<string>(); //foreach (string eachString in strList) //{ // if (!listString.Contains(eachString)) // listString.Add(eachString); //} //var count = db.Database.SqlQuery<string>("SELECT EngineName FROM Web_engine").ToList(); //foreach (string thisString in count) //{ // if (listString.Contains(thisString) || listString.Contains(thisString.Trim())) // listString.Remove(thisString); //} //int jishu = 0; //foreach (string thisString in listString) //{ // //Adddata(thisString); // jishu++; // this.textBox1.Text = jishu.ToString(); // Thread.Sleep(3000); //} } MessageBox.Show("添加完成"); }); } catch (Exception ex) { throw ex; } }
private void Adddata(string engine) { string url = "http://www.hc39.com/desktops/checkEngine.php?key="; string gethtml = HttpGet(url + engine, "text/html;charset=utf-8"); var tbody = gethtml.Split(new string[] { "</tbody>" }, StringSplitOptions.RemoveEmptyEntries); if (tbody.Length > 2) { string name = string.Empty, pingpai = string.Empty, xilie = string.Empty, changshang = string.Empty, qgshu = string.Empty, rnzlei = string.Empty, qgpfxshi = string.Empty, pailiang = string.Empty, pfbzhun = string.Empty, zdscglv = string.Empty, edzshu = string.Empty, zdmli = string.Empty, zdnju = string.Empty, zdnjzshu = string.Empty, spfwei = string.Empty, jqxshi = string.Empty, fdjxshi = string.Empty, qfuhe = string.Empty, fdjjzhong = string.Empty, ysbi = string.Empty, yimiwai = string.Empty, gjXxc = string.Empty, mgqmshu = string.Empty, dhxsu = string.Empty; foreach (var kk in tbody[0].Split(new string[] { "</tr>" }, StringSplitOptions.RemoveEmptyEntries)) { var splist = kk.Split(new string[] { "</td>" }, StringSplitOptions.RemoveEmptyEntries); for (int i = 0; i < splist.Length; i++) { if (splist[i].Contains(@"<td rowspan=""6"" style=""width:360px;""><img src=""")) { Regex regImg = new Regex(@"<img\b[^<>]*?\bsrc[\s\t\r\n]*=[\s\t\r\n]*[""']?[\s\t\r\n]*(?<imgUrl>[^\s\t\r\n""'<>]*)[^<>]*?/?[\s\t\r\n]*>", RegexOptions.IgnoreCase); string imgsrc = string.Empty; // 搜索匹配的字符串 MatchCollection matches = regImg.Matches(splist[i]); foreach (Match match in matches) { imgsrc = match.Groups["imgUrl"].Value; } DownloadPicture(imgsrc, 60000); break; } } } foreach (var x in tbody[1].Split(new string[] { "</tr>" }, StringSplitOptions.RemoveEmptyEntries)) { var list = x.Split(new string[] { "</td>" }, StringSplitOptions.RemoveEmptyEntries); if (list.Length == 1) { continue; } for (int i = 0; i < list.Length; i++) { if (list[i].Contains("发动机:")) { name = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("系列:")) { xilie = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("汽缸数:")) { qgshu = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("排列形式:")) { qgpfxshi = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("排放标准:")) { pfbzhun = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("额定转速:")) { edzshu = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("最大扭矩:")) { zdnju = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("品牌:")) { pingpai = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("厂商:")) { changshang = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("燃料种类:")) { rnzlei = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("排量:")) { pailiang = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("最大输出功率:")) { zdscglv = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("最大马力:")) { zdmli = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("最大扭矩转速:")) { zdnjzshu = list[i + 1].Replace("<td>", ""); } //----------------------------------------- else if (list[i].Contains("适配范围:")) { spfwei = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("进气形式:")) { jqxshi = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("发动机形式:")) { fdjxshi = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("全负荷:")) { qfuhe = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("发动机净重:")) { fdjjzhong = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("压缩比:")) { ysbi = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("一米外噪音:")) { yimiwai = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("缸径x行程:")) { gjXxc = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("每缸气门数:")) { mgqmshu = list[i + 1].Replace("<td>", ""); } else if (list[i].Contains("点火次序:")) { dhxsu = list[i + 1].Replace("<td>", ""); } } } try { using (SqlDbContext insertdb = new SqlDbContext()) { string sql = "insert into [Web_ssp].[dbo].[Web_engine](EngineName,EngineSeries,CylindersNum," + "CylindersArrange,EmissionStandard,Rpm,MaxTorque,EngineBrand,EngineVendor,FuelCategory,Displacement," + "MaxPower,MaxHorsepower,MaxTorqueSpeed,AdaptiveRange,IntakeForm,EngineForm,FullLoad,EngineSuttle,CompressionRatio," + "OneMeterOutside,CylinderXStroke,ValvesPerCylinder,FiringOrder,EnginePic) " + "values('" + name + "','" + xilie + "','" + qgshu + "','" + qgpfxshi + "','" + pfbzhun + "','" + edzshu + "','" + "" + zdnju + "','" + pingpai + "','" + changshang + "','" + rnzlei + "','" + pailiang + "','" + zdscglv + "','" + zdmli + "','" + zdnjzshu + "','" + spfwei + "','" + jqxshi + "','" + fdjxshi + "','" + qfuhe + "','" + fdjjzhong + "','" + ysbi + "','" + yimiwai + "','" + gjXxc + "','" + mgqmshu + "','" + dhxsu + "','" + PicName + "')"; insertdb.Database.ExecuteSqlCommand(sql); } } catch (Exception ex) { throw ex; } } }