/// <summary> /// 开始跑 /// </summary> /// <returns></returns> private static string Run() { MessageLog.AddLog("Run() ===> 开始"); string rate = "NO"; int iAllCount = 0; try { // test list_test //string WebList = "http://www.kinpan.com/kpaward/zz_dk101"; //string ListHtmlTest = HtmlCodeRequest(WebList).Trim(); //List<t_KinpanProList> dtAllProListTest = GetKinpanAllLists(ListHtmlTest); //return rate; string html = GetWebContent(WebUrl).Trim(); List <t_KinpanWard> listWard = GetKinpanMenusLists(html); if (listWard == null) { return(rate); } else { //Thread t2 = new Thread(new ParameterizedThreadStart(PrintNumbers));//有参数的委托 //t2.Start(10); for (int i = 0; i < listWard.Count; i++) { MessageLog.AddLog("开始爬第(" + i + ")个【" + listWard[i].name + "】菜单类型的数据==>"); if (listWard[i].Url.ToString().Length > 0) { string WebUrlList = WebUrl + listWard[i].Url.ToString().TrimStart('/');; string ListHtml = HtmlCodeRequest(WebUrlList).Trim(); List <t_KinpanProList> dtAllProList = GetKinpanAllLists(ListHtml); iAllCount += dtAllProList.Count; MessageLog.AddLog("开始爬第(" + i + ")个菜单类型的数据结束,共计(" + dtAllProList.Count + ")条数据==>"); } } } } catch (Exception ex) { MessageLog.AddErrorLogJson("Run()==>错误:", ex.ToString()); return(rate = "No==>出差错了,已经有(" + iAllCount + ")条"); } return(rate = "OK==>共计(" + iAllCount + ")条数据"); }
/// <summary> /// 开始跑 /// </summary> /// <returns></returns> private static decimal Run() { MessageLog.AddLog("Run() ===> 开始"); decimal rate = 0M; try { // list_test //string WebList = "http://www.kinpan.com/kpaward/bs_dm112"; //string ListHtmlTest = GetWebContent(WebList).Trim(); //List<t_KinpanProList> dtAllProListTest = GetKinpanAllLists(ListHtmlTest); //return rate; string WebUrl = "http://www.kinpan.com/"; string html = GetWebContent(WebUrl).Trim(); List <t_KinpanWard> listWard = GetKinpanMenusLists(html); if (listWard == null) { rate = 0M; } else { //Thread t2 = new Thread(new ParameterizedThreadStart(PrintNumbers));//有参数的委托 //t2.Start(10); for (int i = 0; i < listWard.Count; i++) { if (listWard[i].Url.ToString().Length > 0) { string WebUrlList = WebUrl + listWard[i].Url.ToString().TrimStart('/');; string ListHtml = GetWebContent(WebUrlList).Trim(); List <t_KinpanProList> dtAllProList = GetKinpanAllLists(ListHtml); } } } } catch (Exception ex) { MessageLog.AddLog("Run() ===>" + ex); rate = 0M; } return(rate); }
/// <summary> 获取远程HTML内容</summary> /// <param name="url">远程网页地址URL</param> /// <returns>成功返回远程HTML的代码内容</returns> private static string GetWebContent(string strUrl) { string str = ""; try { WebClient wc = new WebClient(); wc.Credentials = CredentialCache.DefaultCredentials; Encoding enc = Encoding.GetEncoding("UTF-8"); // 如果是乱码就改成 UTF-8 / GB2312 Stream res = wc.OpenRead(strUrl); //以流的形式打开URL StreamReader sr = new StreamReader(res, enc); //以指定的编码方式读取数据流 str = sr.ReadToEnd(); //输出(HTML代码) res.Close(); wc.Dispose(); } catch (Exception ex) { MessageLog.AddLog("GetWebContent() ===>" + ex); return(""); } return(str); }
/// <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> private static List <t_KinpanWard> GetKinpanMenusLists(string strHtml) { List <t_KinpanWard> lKinpanWard = new List <t_KinpanWard>(); try { HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(strHtml); doc.OptionOutputAsXml = true; HtmlNode node = doc.DocumentNode.SelectSingleNode(".//div[@class=\"all-sort-list\"]"); if (node == null) { return(null); } HtmlNodeCollection hrefList = node.SelectNodes(".//*[@class=\"subitem\"]/ul/li/a"); // HtmlNodeCollection hrefNodeList = node.SelectNodes(".//*[@class=\"subitem\"]/ul/li/a[@href]"); //if (hrefNodeList != null) //{ // foreach (var href in hrefNodeList) // { // string name = trNode.InnerText; // HtmlAttribute att = href.Attributes["href"]; // string Url = att.Value; // } //} int thisCount = hrefList.Count; int rid = 0; foreach (HtmlNode strHref in hrefList) { t_KinpanWard mKinpanWard = new t_KinpanWard(); string name = strHref.InnerText.Trim() ?? ""; HtmlAttribute att = strHref.Attributes["href"]; string url = att.Value.Trim() ?? ""; mKinpanWard.name = name; mKinpanWard.Url = url; if (Bllward.AddOrUpdate(mKinpanWard)) { MessageLog.AddLog("添加成功= 共(" + thisCount + ")第(" + rid + ")=>" + JsonConvert.SerializeObject(mKinpanWard)); } else { MessageLog.AddLog("失败--- 共(" + thisCount + ")第(" + rid + ")----" + JsonConvert.SerializeObject(mKinpanWard)); } lKinpanWard.Add(mKinpanWard); rid++; } } catch (Exception ex) { MessageLog.AddLog("GetKinpanMenusLists() ===>" + ex); } return(lKinpanWard); }
/// <summary> /// 下载图片 可写到公用类里 /// </summary> /// <param name="obj">第一个:保存地址;第二个:下载地址</param> /// <returns></returns> public static void DownLoadimg(object obj) { var temps = (List <string>)obj; string simgAllpath = temps[0].ToString(); //保存地址 string sdownUrl = temps[1].ToString(); //下载地址 //初始化清空垃圾 System.GC.Collect(); if (!string.IsNullOrEmpty(sdownUrl)) { try { MessageLog.AddLog("开始下载图片,图片来源:" + sdownUrl + ";图片保存到:" + simgAllpath + ""); if (!sdownUrl.Contains("http")) { sdownUrl = WebUrl + sdownUrl; } System.Net.ServicePointManager.DefaultConnectionLimit = 200; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(sdownUrl); request.Timeout = 10000; request.UserAgent = "User-Agent:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; .NET CLR 1.0.3705"; //request.UserAgent = "User-Agent:Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/45.0.2454.101 Safari/537.36'"; //是否允许302 request.AllowAutoRedirect = true; request.KeepAlive = false; WebResponse response = request.GetResponse(); Stream reader = response.GetResponseStream(); //文件名 //string aFirstName = Guid.NewGuid().ToString(); //扩展名 //string aLastName = url.Substring(url.LastIndexOf(".") + 1, (url.Length - url.LastIndexOf(".") - 1)); string tempPath = simgAllpath.Substring(0, simgAllpath.LastIndexOf('\\')); if (!Directory.Exists(tempPath)) { Directory.CreateDirectory(tempPath); } if (!File.Exists(simgAllpath)) { FileStream writer = new FileStream(simgAllpath, FileMode.OpenOrCreate, FileAccess.Write); byte[] buff = new byte[512]; //实际读取的字节数 int c = 0; while ((c = reader.Read(buff, 0, buff.Length)) > 0) { writer.Write(buff, 0, c); } //释放所有 writer.Close(); writer.Dispose(); reader.Close(); reader.Dispose(); reader = null; //取消请求 request.Abort(); request = null; response.Close(); response = null; } } catch (Exception ex) { //test //WebClient wc = new WebClient(); //wc.DownloadFile(sdownUrl, simgAllpath); MessageLog.AddErrorLogJson("DownLoadimg()==>下载路径:" + sdownUrl + "==> 保存路径:" + simgAllpath + "==>错误:", ex.ToString()); // return "错误:地址" + url; } } // return "错误:地址为空"; }
/// <summary> /// 获取详情 /// </summary> /// <param name="ProDetailsUrl">详情Html地址</param> /// <param name="ImgPath">图片保存地址 如:</param> /// <param name="ProID">项目ID</param> /// <param name="ProName">项目名字</param> /// <returns></returns> public static void GetKinpanDetails(string ProDetailsUrl, string ImgPath, string ProID, string ProName) { try { //string detailsUrl = ProDetailsUrl.ToString().Trim(); //detailsUrl = WebUrl + detailsUrl; //string strHtml = HtmlCodeRequest(detailsUrl).Trim(); HtmlDocument doc = new HtmlDocument(); doc.LoadHtml(ProDetailsUrl); doc.OptionOutputAsXml = true; //轮播图 HtmlNode lbtnode = doc.DocumentNode.SelectSingleNode(".//*[@class=\"list-h\"]"); if (lbtnode != null) { MessageLog.AddLog("轮播图开始=》"); try { ////*[@id="test"]/li[1] HtmlNodeCollection lbtList = lbtnode.SelectNodes(".//li"); foreach (var item in lbtList) { HtmlNodeCollection lablaImg = item.SelectNodes(".//img"); HtmlAttribute ImgUrl = lablaImg[0].Attributes["bigpic"]; string sImgUrl = ImgUrl.Value.Trim() ?? ""; HtmlAttribute Imgtitle = lablaImg[0].Attributes["title"]; // string sImgtitle = Imgtitle.Value ?? ""; string sImgtitle = Imgtitle.Value.Trim().Length > 0 ? Imgtitle.Value.Trim() : ""; var fName = sImgUrl.Split('/').Last(); string tempImgPath = ImgPath + "\\" + fName; List <string> lStr = new List <string>(); lStr.Add(tempImgPath); lStr.Add(sImgUrl); //while ((startDownload.ThreadState != System.Threading.ThreadState.Stopped) && (startDownload.ThreadState != System.Threading.ThreadState.Aborted)) //{ // Thread.Sleep(5000); //} //startDownload.Abort(); //数据保存 t_KinpanImgDetail mKinpanImgDetail = new t_KinpanImgDetail(); mKinpanImgDetail.ProID = ProID; mKinpanImgDetail.ProImgSavaPath = tempImgPath; mKinpanImgDetail.ProImgTitle = sImgtitle; mKinpanImgDetail.ProImgType = 0; mKinpanImgDetail.ProImgUrl = sImgUrl; if (BllImgDetails.AddOrUpdate(mKinpanImgDetail)) { MessageLog.AddLog("添加成功,轮播图图片下载地址(" + sImgUrl + ");轮播图图片保存地址(" + tempImgPath + ")。"); Thread startDownload = new Thread(new ParameterizedThreadStart(DownLoadimg)); startDownload.Start(lStr); } else { MessageLog.AddLog("添加失败,轮播图图片下载地址(" + sImgUrl + ");轮播图图片保存地址(" + tempImgPath + ")。"); } } } catch (Exception ex) { MessageLog.AddLog("轮播图出错=》" + ex); } MessageLog.AddLog("轮播图结束=》"); } //项目详情 HtmlNode xmxqnode = doc.DocumentNode.SelectSingleNode(".//div[@class=\"QXCon2_c1xmxq\"]"); if (xmxqnode != null) { MessageLog.AddLog("开始保存项目信息详情"); //项目信息详情 HtmlNodeCollection xmxxxq = xmxqnode.SelectNodes(".//div[@class=\"QXCon2c_con\"]/ul/li"); string szhi = ""; foreach (var item in xmxxxq) { szhi += item.InnerHtml.Trim() + " ; "; //数据保存 } List <string> lxqStr = new List <string>(); lxqStr.Add(ImgPath); lxqStr.Add(szhi); lxqStr.Add("项目信息详情"); //描述 //string sms = ""; //HtmlNodeCollection ms = xmxqnode.SelectNodes(".//div[@class=\"QXCon2_clpic\"]/p"); //foreach (var item in ms) //{ // sms += item.InnerHtml.Trim(); //} //图片 //描述 //*[@id="js_detail"] HtmlNodeCollection picDetail = xmxqnode.SelectNodes(".//*[@id=\"js_detail\"]"); if (picDetail[0].InnerHtml.Trim().Length == 0) { picDetail = xmxqnode.SelectNodes(".//div[@class=\"QXCon2_clpic\"]"); } string sms = ""; //string(.) foreach (var picImg in picDetail) { HtmlNodeCollection mss = picImg.SelectNodes(".//p"); //只可能有一个 if (mss.Count > 0) { foreach (var item in mss) { sms += item.InnerText.Trim() + "、"; } } List <string> lmsStr = new List <string>(); lmsStr.Add(ImgPath); lmsStr.Add(sms); lmsStr.Add("描述"); Thread startMsinfoLog = new Thread(new ParameterizedThreadStart(InfoLog)); startMsinfoLog.Start(lmsStr); HtmlNodeCollection lablaImg = picImg.SelectNodes(".//img"); foreach (var PicImgs in lablaImg) { HtmlAttribute ImgUrl = PicImgs.Attributes["src"]; // string sImgUrl = ImgUrl.Value.Trim() ?? ""; string sImgUrl = ImgUrl.Value.Trim() ?? ""; HtmlAttribute Imgtitle = PicImgs.Attributes["title"]; // string sImgtitle = Imgtitle.Value.Trim() ?? ""; string sImgtitle = ""; try { sImgtitle = Imgtitle.Value.Trim().Length > 0 ? Imgtitle.Value.Trim() : ""; } catch (Exception) { } var fName = sImgUrl.Split('/').Last(); string tempImgPath = ImgPath + "\\" + fName; List <string> lStr = new List <string>(); lStr.Add(tempImgPath); lStr.Add(sImgUrl); t_KinpanImgDetail mKinpanImgDetail = new t_KinpanImgDetail(); mKinpanImgDetail.ProID = ProID; mKinpanImgDetail.ProImgSavaPath = tempImgPath; mKinpanImgDetail.ProImgTitle = sImgtitle; mKinpanImgDetail.ProImgType = 1; mKinpanImgDetail.ProImgUrl = sImgUrl; if (BllImgDetails.AddOrUpdate(mKinpanImgDetail)) { MessageLog.AddLog("添加成功,图片下载地址(" + sImgUrl + ");图片保存地址(" + tempImgPath + ")。"); Thread startDownload = new Thread(new ParameterizedThreadStart(DownLoadimg)); startDownload.Start(lStr); } else { MessageLog.AddLog("添加失败,图片下载地址(" + sImgUrl + ");图片保存地址(" + tempImgPath + ")。"); } } Thread startinfoLog = new Thread(new ParameterizedThreadStart(InfoLog)); startinfoLog.Start(lxqStr); } t_KinpanDetails mKinpanDetails = new t_KinpanDetails(); mKinpanDetails.ProID = ProID; mKinpanDetails.ProName = ProName; mKinpanDetails.ProTextDtails = szhi; mKinpanDetails.ProDescription = sms; mKinpanDetails.CreationAt = System.DateTime.Now; if (BllDetails.AddOrUpdate(mKinpanDetails)) { MessageLog.AddLog("成功数据==》" + JsonConvert.SerializeObject(mKinpanDetails)); MessageLog.AddLog("项目详情添加成功"); } else { MessageLog.AddLog("失败数据==》" + JsonConvert.SerializeObject(mKinpanDetails)); MessageLog.AddLog("项目详情添加失败"); } MessageLog.AddLog("添加项目详情==>"); } } catch (Exception ex) { MessageLog.AddErrorLogJson("GetKinpanDetails()==>错误:", ex.ToString()); } }
/// <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); }
/// <summary> /// 服务器 /// </summary> /// <param name="file"></param> /// <returns></returns> public JsonResult ImportServiceExcelFile(HttpPostedFileBase file) { // string filePath = RequestParameters.Pstring("filePath"); if (!Utits.IsLogin) { return(Json(new { jsonrpc = 2.0, error = new { code = 102, message = "登录状态已失效." } })); } string filePathName = string.Empty; string localPath = Server.MapPath(string.Format("/{0}/ServiceData/", Utits.UploadExcelPath)); if (Request.Files.Count == 0) { return(Json(new { jsonrpc = 2.0, error = new { code = 100, message = "文件上传失败." } })); } // string ex = Path.GetExtension(file.FileName); filePathName = DateTime.Now.ToString("yyyyMMddHHssmm") + "_" + file.FileName; if (!System.IO.Directory.Exists(localPath)) { System.IO.Directory.CreateDirectory(localPath); } file.SaveAs(localPath + filePathName); #region 信息 try { var currentUser = Utits.CurrentUser; var OpUserId = currentUser.user_Id; var Opdate = System.DateTime.Now; var mdate = DateTime.Parse("1999/09/09"); var cBll = new BLL_Service(); List <td_Service_1> list = new List <td_Service_1>(); List <td_Service_2> list_2 = new List <td_Service_2>(); using (FileStream fsfile = new FileStream(localPath + filePathName, FileMode.Open, FileAccess.Read)) { XSSFWorkbook workbook = new XSSFWorkbook(fsfile); ISheet sheet_1 = workbook.GetSheetAt(0); //第一个工作表 ISheet sheet_2 = workbook.GetSheetAt(1); ///Excel 第一行是标题,不需要导入数据库的 for (int i = 1; i <= sheet_1.LastRowNum; i++) { IRow row = sheet_1.GetRow(i); td_Service_1 service1 = new td_Service_1(); //第一列不管 if (row.GetCell(1) == null) { service1.fwqbh = ""; } else { service1.fwqbh = row.GetCell(1).StringCellValue; } if (row.GetCell(2) == null) { service1.xh = ""; } else { service1.xh = row.GetCell(2).StringCellValue; } if (row.GetCell(3) == null) { service1.gmrq = mdate; } else { service1.gmrq = row.GetCell(3).DateCellValue; } if (row.GetCell(4) == null) { service1.gbrq = mdate; } else { service1.gbrq = row.GetCell(4).DateCellValue; } if (row.GetCell(5) == null) { service1.cpbb = ""; } else { service1.cpbb = row.GetCell(5).StringCellValue; } if (row.GetCell(6) == null) { service1.xlh = ""; } else { service1.xlh = row.GetCell(6).NumericCellValue.ToString(); } if (row.GetCell(7) == null) { service1.gdzcbh = ""; } else { service1.gdzcbh = row.GetCell(7).StringCellValue; } if (row.GetCell(8) == null) { service1.wlwz = ""; } else { service1.wlwz = row.GetCell(8).StringCellValue; } if (row.GetCell(9) == null) { service1.jgwz = ""; } else { service1.jgwz = row.GetCell(9).StringCellValue; } if (row.GetCell(10) == null) { service1.IPdz = ""; } else { service1.IPdz = row.GetCell(10).StringCellValue; } if (row.GetCell(11) == null) { service1.ym = ""; } else { service1.ym = row.GetCell(11).StringCellValue; } if (row.GetCell(12) == null) { service1.wg = ""; } else { service1.wg = row.GetCell(12).StringCellValue; } if (row.GetCell(13) == null) { service1.jqm = ""; } else { service1.jqm = row.GetCell(13).StringCellValue; } if (row.GetCell(14) == null) { service1.zyywyy = ""; } else { service1.zyywyy = row.GetCell(14).StringCellValue; } if (row.GetCell(15) == null) { service1.czxt = ""; } else { service1.czxt = row.GetCell(15).StringCellValue; } if (row.GetCell(16) == null) { service1.sjkxt = ""; } else { service1.sjkxt = row.GetCell(16).StringCellValue; } if (row.GetCell(17) == null) { service1.CPU = ""; } else { service1.CPU = row.GetCell(17).StringCellValue; } if (row.GetCell(18) == null) { service1.nc = ""; } else { service1.nc = row.GetCell(18).StringCellValue; } if (row.GetCell(19) == null) { service1.yp = ""; } else { service1.yp = row.GetCell(19).StringCellValue; } if (row.GetCell(20) == null) { service1.ywsj = ""; } else { service1.ywsj = row.GetCell(20).StringCellValue; } if (row.GetCell(21) == null) { service1.sfrb = ""; } else { service1.sfrb = row.GetCell(21).StringCellValue; } if (row.GetCell(22) == null) { service1.zycd = ""; } else { service1.zycd = row.GetCell(22).StringCellValue; } if (row.GetCell(23) == null) { service1.glyhm = ""; } else { service1.glyhm = row.GetCell(23).StringCellValue; } if (row.GetCell(24) == null) { service1.glmm = ""; } else { service1.glmm = row.GetCell(24).StringCellValue; } if (row.GetCell(25) == null) { service1.yhm1 = ""; } else { service1.yhm1 = row.GetCell(25).StringCellValue; } if (row.GetCell(26) == null) { service1.mm1 = ""; } else { service1.mm1 = row.GetCell(26).StringCellValue; } if (row.GetCell(27) == null) { service1.xlhbbgl = ""; } else { service1.xlhbbgl = row.GetCell(27).StringCellValue; } if (row.GetCell(28) == null) { service1.sfjh = ""; } else { service1.sfjh = row.GetCell(28).StringCellValue; } if (row.GetCell(29) == null) { service1.jhm = ""; } else { service1.jhm = row.GetCell(29).StringCellValue; } if (row.GetCell(30) == null) { service1.sykyxjhsl = 0; } else { service1.sykyxjhsl = Convert.ToInt32(row.GetCell(30).NumericCellValue); } if (row.GetCell(31) == null) { service1.sfzb = ""; } else { service1.sfzb = row.GetCell(31).StringCellValue; } service1.Dept_Name = ""; service1.OperatTime = Opdate; service1.OperatPid = OpUserId; list.Add(service1); } for (int i = 1; i <= sheet_2.LastRowNum; i++) { IRow row = sheet_2.GetRow(i); td_Service_2 service2 = new td_Service_2(); if (row.GetCell(1) == null) { service2.IPdz = ""; } else { service2.IPdz = row.GetCell(1).StringCellValue; } if (row.GetCell(2) == null || row.GetCell(2).ToString() == "") { service2.tjsj = mdate; } else { service2.tjsj = row.GetCell(2).DateCellValue; } if (row.GetCell(3) == null) { service2.xnjm = ""; } else { service2.xnjm = row.GetCell(3).StringCellValue; } if (row.GetCell(4) == null) { service2.Dept_Name = ""; } else { service2.Dept_Name = row.GetCell(4).StringCellValue; } if (row.GetCell(5) == null) { service2.xtmc = ""; } else { service2.xtmc = row.GetCell(5).StringCellValue; } if (row.GetCell(6) == null) { service2.bz = ""; } else { service2.bz = row.GetCell(6).StringCellValue; } if (row.GetCell(7) == null) { service2.CPU = ""; } else { service2.CPU = row.GetCell(7).StringCellValue; } if (row.GetCell(8) == null) { service2.nc = ""; } else { service2.nc = row.GetCell(8).StringCellValue; } if (row.GetCell(9) == null) { service2.ypkj = ""; } else { service2.ypkj = row.GetCell(9).StringCellValue; } if (row.GetCell(10) == null) { service2.czxt = ""; } else { service2.czxt = row.GetCell(10).StringCellValue; } if (row.GetCell(11) == null) { service2.glyhm = ""; } else { service2.glyhm = row.GetCell(11).StringCellValue; } if (row.GetCell(12) == null) { service2.glmm = ""; } else { service2.glmm = row.GetCell(12).StringCellValue; } if (row.GetCell(13) == null) { service2.whyhm1 = ""; } else { service2.whyhm1 = row.GetCell(13).StringCellValue; } if (row.GetCell(14) == null) { service2.whmm1 = ""; } else { service2.whmm1 = row.GetCell(14).StringCellValue; } if (row.GetCell(15) == null) { service2.sjyhm = ""; } else { service2.sjyhm = row.GetCell(15).StringCellValue; } if (row.GetCell(16) == null) { service2.sjmm = ""; } else { service2.sjmm = row.GetCell(16).StringCellValue; } //Room_sl.OperatTime = Opdate; //Room_sl.OperatPid = OpUserId; list_2.Add(service2); } if (cBll.AddImport_1(list) && cBll.AddImport_2(list_2)) { return(Json(new { jsonrpc = 2.0, message = "导入成功." })); } else { return(Json(new { jsonrpc = 2.0, error = new { code = 100, message = "导入失败." } })); } } } catch (Exception e) { MessageLog.AddLog(string.Format("UpDataProcess(数据导入)异常:{0}", e.Message)); } return(Json(new { jsonrpc = 2.0, error = new { code = 100, message = "系统错误.(请确保数据完整)" } })); #endregion }
/// <summary> /// 安全设备 /// </summary> /// <param name="file"></param> /// <returns></returns> public JsonResult ImportSecurityExcelFile(HttpPostedFileBase file) { // string filePath = RequestParameters.Pstring("filePath"); if (!Utits.IsLogin) { return(Json(new { jsonrpc = 2.0, error = new { code = 102, message = "登录状态已失效." } })); } string filePathName = string.Empty; string localPath = Server.MapPath(string.Format("/{0}/SecurityData/", Utits.UploadExcelPath)); if (Request.Files.Count == 0) { return(Json(new { jsonrpc = 2.0, error = new { code = 100, message = "文件上传失败." } })); } // string ex = Path.GetExtension(file.FileName); filePathName = DateTime.Now.ToString("yyyyMMddHHssmm") + "_" + file.FileName; if (!System.IO.Directory.Exists(localPath)) { System.IO.Directory.CreateDirectory(localPath); } file.SaveAs(localPath + filePathName); #region 信息 try { var currentUser = Utits.CurrentUser; var OpUserId = currentUser.user_Id; var Opdate = System.DateTime.Now; var mdate = DateTime.Parse("1999/09/09"); var cBll = new BLL_Security(); List <td_Security_1> list = new List <td_Security_1>(); List <td_Security_xxsl_1> list_xxsl = new List <td_Security_xxsl_1>(); using (FileStream fsfile = new FileStream(localPath + filePathName, FileMode.Open, FileAccess.Read)) { XSSFWorkbook workbook = new XSSFWorkbook(fsfile); // HSSFWorkbook Hworkbook = new HSSFWorkbook(fsfile); NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0); //第一个工作表 //NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(1); //第二个工作表 ISheet sheet_xxsl = workbook.GetSheet("设备信息"); // string nam = workbook.GetSheetName(1); ///Excel 第一行是标题,不需要导入数据库的 for (int i = 1; i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); td_Security_1 security = new td_Security_1(); if (row.GetCell(0) == null) { security.Dept_Name = ""; } else { security.Dept_Name = row.GetCell(0).StringCellValue; } if (row.GetCell(1) == null) { security.bswz = ""; } else { security.bswz = row.GetCell(1).StringCellValue; } if (row.GetCell(2) == null) { security.dyxt = ""; } else { security.dyxt = row.GetCell(2).StringCellValue; } if (row.GetCell(3) == null) { security.sfjrzwww = ""; } else { security.sfjrzwww = row.GetCell(3).StringCellValue; } if (row.GetCell(4) == null) { security.sbglryxm = ""; } else { security.sbglryxm = row.GetCell(4).StringCellValue; } if (row.GetCell(5) == null) { security.lxfs = 0; } else { security.lxfs = Convert.ToInt32(row.GetCell(5).NumericCellValue); } security.OperatTime = Opdate; security.OperatPid = OpUserId; list.Add(security); } for (int i = 1; i <= sheet_xxsl.LastRowNum; i++) { IRow row = sheet_xxsl.GetRow(i); td_Security_xxsl_1 security_sl = new td_Security_xxsl_1(); if (row.GetCell(0) == null) { security_sl.Dept_Name = ""; } else { security_sl.Dept_Name = row.GetCell(0).StringCellValue; } if (row.GetCell(1) == null) { security_sl.aqsbhrjmc = ""; } else { security_sl.aqsbhrjmc = row.GetCell(1).StringCellValue; } if (row.GetCell(2) == null) { security_sl.sfbs = ""; } else { security_sl.sfbs = row.GetCell(2).StringCellValue; } if (row.GetCell(3) == null) { security_sl.sl = 0; } else { security_sl.sl = Convert.ToInt32(row.GetCell(3).NumericCellValue); } if (row.GetCell(4) == null) { security_sl.pp = ""; } else { security_sl.pp = row.GetCell(4).StringCellValue; } if (row.GetCell(5) == null) { security_sl.xh = ""; } else { security_sl.xh = row.GetCell(5).StringCellValue; } if (row.GetCell(6) == null) { security_sl.gmrq = mdate; } else { security_sl.gmrq = row.GetCell(6).DateCellValue; } if (row.GetCell(7) == null) { security_sl.gmjq = 0; } else { security_sl.gmjq = Convert.ToInt32(row.GetCell(7).NumericCellValue); } //Room_sl.OperatTime = Opdate; //Room_sl.OperatPid = OpUserId; list_xxsl.Add(security_sl); } if (cBll.AddImport(list) && cBll.AddImport_sl(list_xxsl)) { return(Json(new { jsonrpc = 2.0, message = "导入成功." })); } else { return(Json(new { jsonrpc = 2.0, error = new { code = 100, message = "导入失败." } })); } } } catch (Exception e) { MessageLog.AddLog(string.Format("UpDataProcess(数据导入)异常:{0}", e.Message)); } return(Json(new { jsonrpc = 2.0, error = new { code = 100, message = "系统错误.(请确保数据完整)" } })); #endregion }
// GET: Import //public ActionResult Index() //{ // return View(); //} /// <summary> /// 网络情况 /// </summary> /// <param name="filePath"></param> /// <returns></returns> public JsonResult ImportNetExcelFile(HttpPostedFileBase file) { // string filePath = RequestParameters.Pstring("filePath"); if (!Utits.IsLogin) { return(Json(new { jsonrpc = 2.0, error = new { code = 102, message = "登录状态已失效." } })); } string filePathName = string.Empty; string localPath = Server.MapPath(string.Format("/{0}/NetWorkData/", Utits.UploadExcelPath)); if (Request.Files.Count == 0) { return(Json(new { jsonrpc = 2.0, error = new { code = 100, message = "文件上传失败." } })); } // string ex = Path.GetExtension(file.FileName); filePathName = DateTime.Now.ToString("yyyyMMddHHssmm") + "_" + file.FileName; if (!System.IO.Directory.Exists(localPath)) { System.IO.Directory.CreateDirectory(localPath); } file.SaveAs(localPath + filePathName); #region 信息 try { var currentUser = Utits.CurrentUser; var OpUserId = currentUser.user_Id; var Opdate = System.DateTime.Now; var cBll = new BLL_Network(); List <td_Network_1> list = new List <td_Network_1>(); using (FileStream fsfile = new FileStream(localPath + filePathName, FileMode.Open, FileAccess.Read)) { XSSFWorkbook workbook = new XSSFWorkbook(fsfile); NPOI.SS.UserModel.ISheet sheet = workbook.GetSheetAt(0); //第一个工作表 ///Excel 第一行是标题,不需要导入数据库的 for (int i = 1; i <= sheet.LastRowNum; i++) { IRow row = sheet.GetRow(i); td_Network_1 network = new td_Network_1(); if (row.GetCell(0) == null) { network.Dept_Name = ""; } else { network.Dept_Name = row.GetCell(0).StringCellValue; } if (row.GetCell(1) == null) { network.bgdd = ""; } else { network.bgdd = row.GetCell(1).StringCellValue; } if (row.GetCell(2) == null) { network.lxdh = ""; } else { network.lxdh = row.GetCell(2).StringCellValue; } if (row.GetCell(3) == null) { network.jrzwwwzdsl = 0; } else { network.jrzwwwzdsl = Convert.ToInt32(row.GetCell(3).NumericCellValue); } if (row.GetCell(4) == null) { network.jrgwwzdsl = 0; } else { network.jrgwwzdsl = Convert.ToInt32(row.GetCell(4).NumericCellValue); } if (row.GetCell(5) == null) { network.jrzwwwIPdzd = ""; } else { network.jrzwwwIPdzd = row.GetCell(5).StringCellValue; } if (row.GetCell(6) == null) { network.scjhj = 0; } else { network.scjhj = Convert.ToInt32(row.GetCell(6).NumericCellValue); } if (row.GetCell(7) == null) { network.ecjhj = 0; } else { network.ecjhj = Convert.ToInt32(row.GetCell(7).NumericCellValue); } if (row.GetCell(8) == null) { network.lyq = 0; } else { network.lyq = Convert.ToInt32(row.GetCell(8).NumericCellValue); } if (row.GetCell(9) == null) { network.gdj = 0; } else { network.gdj = Convert.ToInt32(row.GetCell(9).NumericCellValue); } if (row.GetCell(10) == null) { network.llfzjh = 0; } else { network.llfzjh = Convert.ToInt32(row.GetCell(10).NumericCellValue); } if (row.GetCell(11) == null) { network.gx = 0; } else { network.gx = Convert.ToInt32(row.GetCell(11).NumericCellValue); } if (row.GetCell(12) == null) { network.wxjrd = 0; } else { network.wxjrd = Convert.ToInt32(row.GetCell(12).NumericCellValue); } if (row.GetCell(13) == null) { network.wxzj = 0; } else { network.wxzj = Convert.ToInt32(row.GetCell(13).NumericCellValue); } if (row.GetCell(14) == null) { network.flwck = ""; } else { network.flwck = row.GetCell(14).StringCellValue; } network.OperatTime = Opdate; network.OperatPid = OpUserId; list.Add(network); } if (cBll.AddImportNet(list)) { return(Json(new { jsonrpc = 2.0, message = "导入成功." })); } else { return(Json(new { jsonrpc = 2.0, error = new { code = 100, message = "导入失败." } })); } } } catch (Exception e) { MessageLog.AddLog(string.Format("UpDataProcess(数据导入)异常:{0}", e.Message)); } return(Json(new { jsonrpc = 2.0, error = new { code = 100, message = "系统错误.(请确保数据完整)" } })); #endregion }