public bool AddOrUpdate(t_KinpanProList entity) { if (entity.id < 1) { return(dbContext.Add(entity)); } return(dbContext.Update(entity, c => c.id == entity.id)); }
/// <summary> /// 获取Kinpan单个类型全部项目List /// </summary> /// <param name="strHtml"></param> /// <returns></returns> public static List <t_KinpanProList> GetKinpanAllLists(string strHtml) { List <t_KinpanProList> lKinpanProList = new List <t_KinpanProList>(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(strHtml); doc.OptionOutputAsXml = true; HtmlNode node = doc.DocumentNode.SelectSingleNode(".//div[@class=\"SCon_pic03\"]"); if (node == null) { return(null); } HtmlNodeCollection ProList = node.SelectNodes(".//*[@class=\"con_list_wrap\"]/li"); //翻页 HtmlNodeCollection fy = node.SelectNodes(".//div[@class=\"SCon_picfy\"]/p"); string yeCount = fy[0].InnerText.TrimStart('共'); yeCount = yeCount.Substring(0, 5); string Count = Regex.Replace(yeCount, @"[^0-9]+", ""); int iCount = int.Parse(Count); //当前curpage HtmlNodeCollection thisYe = node.SelectNodes(".//a[@class=\"curpage\"]"); string sthisYe = thisYe[0].InnerText.Trim(); sthisYe = Regex.Replace(sthisYe, @"[^0-9]+", ""); int ithisYe = int.Parse(sthisYe); //当前search url HtmlNodeCollection url = node.SelectNodes(".//div[@class=\"SCon_picfy\"]/p/a"); HtmlAttribute urltemp = url[0].Attributes["href"]; string Surltemp = urltemp.Value.Trim() ?? ""; //Surltemp = Regex.Replace(Surltemp, @"\\d + ", ""); //Regex r = new Regex("\\d+"); //var ms = r.Matches(Surltemp); //if (ms.Count > 0) // ms.OfType<Match>().Last(); Surltemp = Surltemp.Remove(Surltemp.Length - 1, 1);//移除最后数字 (十位数有问题) // return lKinpanProList; if (iCount != ithisYe) { string WebUrl = "http://www.kinpan.com/" + urltemp + (ithisYe + 1); string ListHtml = GetWebContent(WebUrl).Trim(); GetKinpanAllLists(ListHtml); } int thisCount = ProList.Count; int rid = 0; foreach (HtmlNode pro in ProList) { t_KinpanProList mKinpanProList = new t_KinpanProList(); ////得到第一个A标签 HtmlNodeCollection lablaA = pro.SelectNodes(".//a"); HtmlAttribute Name = lablaA[1].Attributes["title"]; string sName = Name.Value.Trim() ?? ""; HtmlAttribute ProUrl = lablaA[0].Attributes["href"]; string sProUrl = ProUrl.Value.Trim() ?? ""; HtmlAttribute gongsiUrl = lablaA[2].Attributes["href"]; string sgongsiUrl = gongsiUrl.Value.Trim() ?? ""; string sgongsi = lablaA[2].InnerText; HtmlNodeCollection jiage = pro.SelectNodes(".//span[@class=\"singlep\"]"); string sjiage = jiage[0].InnerText.Trim(); string jiageType = jiage[0].InnerText.Trim(); sjiage = Regex.Replace(sjiage, @"[^0-9]+", ""); float fjiage = float.Parse(sjiage); int jiagelx = jiageType.Substring(0, 4) == "销售单价" ? 1 : 0; HtmlNodeCollection liulan = pro.SelectNodes(".//span[@class=\"browse\"]"); string sliulan = liulan[0].InnerText.Trim(); sliulan = Regex.Replace(sliulan, @"[^0-9]+", ""); int iliulan = int.Parse(sliulan); HtmlNodeCollection lablaImg = pro.SelectNodes(".//img"); HtmlAttribute img = lablaImg[0].Attributes["src"]; string simgurl = img.Value.Trim() ?? ""; HtmlAttribute Typeimg = lablaImg[1].Attributes["src"]; string sType = Typeimg.Value.Trim() ?? ""; var temp = sType.Split('.'); sType = temp[0].Substring(temp[0].Length - 3) == "qjd" ? "旗舰店" : "专营店"; ////得到第一个Img //HtmlNodeCollection lablaImg = pro.SelectNodes(".//a[1]/img"); //HtmlAttribute ImgUrl = lablaImg.Attributes["src"]; //string sImgUrl = ImgUrl.Value; mKinpanProList.ProName = sName; mKinpanProList.SalesPrice = fjiage; mKinpanProList.SalesPriceType = jiagelx; mKinpanProList.Views = iliulan; mKinpanProList.ProImgUrl = simgurl; mKinpanProList.DesignCompany = sgongsi; mKinpanProList.DesignCompanyUrl = sgongsiUrl; mKinpanProList.CompanyType = sType; mKinpanProList.ProDetailsUrl = sProUrl; var fName = simgurl.Split('/'); // FileUtils.SaveFile(simgurl, fName.Last()); mKinpanProList.ProImgSavePath = FileUtils.SaveFile(simgurl, sName + "\\" + fName.Last()); if (BllProList.AddOrUpdate(mKinpanProList)) { MessageLog.AddLog("添加成功= 共(" + thisCount + ")第(" + rid + ")=>" + JsonConvert.SerializeObject(mKinpanProList)); } else { MessageLog.AddLog("失败--- 共(" + thisCount + ")第(" + rid + ")----" + JsonConvert.SerializeObject(mKinpanProList)); } //详情 string WebUrl = "http://www.kinpan.com/" + sProUrl; string ListHtml = GetWebContent(WebUrl).Trim(); GetKinpanDatail(ListHtml); rid++; } return(lKinpanProList); }
/// <summary> /// 获取Kinpan单个类型全部项目List /// </summary> /// <param name="strHtml"></param> /// <returns></returns> public static List <t_KinpanProList> GetKinpanAllLists(string strHtml) { List <t_KinpanProList> lKinpanProList = new List <t_KinpanProList>(); if (strHtml.Length > 0) { try { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(strHtml); doc.OptionOutputAsXml = true; HtmlNode node = doc.DocumentNode.SelectSingleNode(".//div[@class=\"SCon_pic03\"]"); if (node == null) { return(null); } HtmlNodeCollection ProList = node.SelectNodes(".//*[@class=\"con_list_wrap\"]/li"); int thisCount = ProList.Count; int rid = 0; MessageLog.AddLog("开始添加当前页的项目集合==>"); foreach (HtmlNode pro in ProList) { t_KinpanProList mKinpanProList = new t_KinpanProList(); ////得到第一个A标签 HtmlNodeCollection lablaA = pro.SelectNodes(".//a"); HtmlAttribute Name = lablaA[1].Attributes["title"]; string sName = Name.Value.Trim() ?? ""; HtmlAttribute ProUrl = lablaA[0].Attributes["href"]; string sProUrl = ProUrl.Value.Trim() ?? ""; HtmlAttribute gongsiUrl = lablaA[2].Attributes["href"]; string sgongsiUrl = gongsiUrl.Value.Trim() ?? ""; string sgongsi = lablaA[2].InnerText; HtmlNodeCollection jiage = pro.SelectNodes(".//span[@class=\"singlep\"]"); string sjiage = jiage[0].InnerText.Trim(); string jiageType = jiage[0].InnerText.Trim(); sjiage = Regex.Replace(sjiage, @"[^0-9]+", ""); float fjiage = float.Parse(sjiage); int jiagelx = jiageType.Substring(0, 4) == "销售单价" ? 1 : 0; HtmlNodeCollection liulan = pro.SelectNodes(".//span[@class=\"browse\"]"); string sliulan = liulan[0].InnerText.Trim(); sliulan = Regex.Replace(sliulan, @"[^0-9]+", ""); int iliulan = int.Parse(sliulan); HtmlNodeCollection lablaImg = pro.SelectNodes(".//img"); HtmlAttribute img = lablaImg[0].Attributes["src"]; string simgurl = img.Value.Trim() ?? ""; HtmlAttribute Typeimg = lablaImg[1].Attributes["src"]; string sType = Typeimg.Value.Trim() ?? ""; var temp = sType.Split('.'); sType = temp[0].Substring(temp[0].Length - 3) == "qjd" ? "旗舰店" : "专营店"; ////得到第一个Img //HtmlNodeCollection lablaImg = pro.SelectNodes(".//a[1]/img"); //HtmlAttribute ImgUrl = lablaImg.Attributes["src"]; //string sImgUrl = ImgUrl.Value; string sGProId = Guid.NewGuid().ToString(); mKinpanProList.ProName = sName; mKinpanProList.ProID = sGProId; mKinpanProList.SalesPrice = fjiage; mKinpanProList.SalesPriceType = jiagelx; mKinpanProList.Views = iliulan; mKinpanProList.ProImgUrl = simgurl; mKinpanProList.DesignCompany = sgongsi; mKinpanProList.DesignCompanyUrl = sgongsiUrl; mKinpanProList.CompanyType = sType; mKinpanProList.ProDetailsUrl = sProUrl; var fName = simgurl.Split('/'); // FileUtils.SaveFile(simgurl, fName.Last()); //图片保存 路径+公司+项目; string PathGongsi = ImgPath + sgongsi + "\\" + sName; string simgpath = PathGongsi + "\\" + fName.Last(); //mKinpanProList.ProImgSavePath = FileUtils.SaveFile(simgurl, sName + "\\" + fName.Last()); mKinpanProList.ProImgSavePath = simgpath; if (BllProList.AddOrUpdate(mKinpanProList)) { List <string> lStr = new List <string>(); lStr.Add(simgpath); lStr.Add(simgurl); Thread startDownload = new Thread(new ParameterizedThreadStart(DownLoadimg)); startDownload.Start(lStr); MessageLog.AddLog("添加成功= 共(" + thisCount + ")第(" + rid + ")=>" + JsonConvert.SerializeObject(mKinpanProList)); } else { MessageLog.AddLog("失败--- 共(" + thisCount + ")第(" + rid + ")----" + JsonConvert.SerializeObject(mKinpanProList)); } //详情 string tempWebUrl = WebUrl + sProUrl; string ListHtml = HtmlCodeRequest(tempWebUrl).Trim(); GetKinpanDetails(ListHtml, PathGongsi, sGProId, sName); rid++; } #region 用于递归 //翻页 HtmlNodeCollection fy = node.SelectNodes(".//div[@class=\"SCon_picfy\"]/p"); string yeCount = fy[0].InnerText.TrimStart('共'); yeCount = yeCount.Substring(0, 5); string Count = Regex.Replace(yeCount, @"[^0-9]+", ""); int iCount = int.Parse(Count); //当前curpage HtmlNodeCollection thisYe = node.SelectNodes(".//a[@class=\"curpage\"]"); string sthisYe = thisYe[0].InnerText.Trim(); sthisYe = Regex.Replace(sthisYe, @"[^0-9]+", ""); int ithisYe = int.Parse(sthisYe); //当前search url HtmlNodeCollection url = node.SelectNodes(".//div[@class=\"SCon_picfy\"]/p/a"); HtmlAttribute urltemp = url[0].Attributes["href"]; string Surltemp = urltemp.Value.Trim() ?? ""; // kpaward/search/12?stp=&dt=&lm=&g=112&sc=&st=&f=&ht=&ha=&sort=3 //添加 &page=? // Surltemp = Surltemp.Substring(0, Surltemp.LastIndexOf("="));//移除最后数字 (十位数有问题) if (ithisYe <= (iCount + 1)) { string tepWebUrl = WebUrl + Surltemp + "&page=" + (ithisYe + 1); string ListHtml = HtmlCodeRequest(tepWebUrl).Trim(); GetKinpanAllLists(ListHtml); } #endregion MessageLog.AddLog("共(" + iCount + ")页,当前页是(" + ithisYe + "),==>"); } catch (Exception ex) { MessageLog.AddErrorLogJson("GetKinpanAllLists()==>错误:", ex.ToString()); } } return(lKinpanProList); }