void OnTimedEvent() { using (var ctx = new oaEntities()) { //——————————————————————更新 时间大于 var ftimes = DateTime.Now; var catemp = ctx.T_FGJHtmlData.Where(x => x.FbTime > ftimes).DefaultIfEmpty(); if (catemp != null) { if (catemp.Count() > 0) { foreach (var tp in catemp) { long id = tp.ID; using (var Uptimes = new oaEntities()) { var tps = Uptimes.T_FGJHtmlData.FirstOrDefault(z => z.ID == id); tps.delflag = 1; Uptimes.SaveChanges(); } } } } //———————————————————————————————————— int Gint = 0; this.Dispatcher.Invoke(new Action(() => { Gint = Convert.ToInt32(TiaDay.Text); })); var Zuijia = ctx.T_ZhuaiJiaBak.Where(x => x.DEL == 0).ToList(); List <long> lit = new List <long>(); foreach (var a in Zuijia) { lit.Add(Convert.ToInt64(a.FGJHTML_id)); } var T_saverHtml = ctx.T_SaveHtmlData.Where(x => x.DelFlag == 0 && x.GongGong == 0 && !lit.Contains(x.HtmldataID)).ToList(); for (int i = 0; i < T_saverHtml.Count(); i++) { if (T_saverHtml[i].SaveTime.AddDays(Gint) < DateTime.Now) { long id = T_saverHtml[i].ID; using (var newoa = new oaEntities()) { var temp = newoa.T_SaveHtmlData.FirstOrDefault(x => x.ID == id); temp.GongGong = 1; // newoa.Entry<T_SaveHtmlData>(temp).State = System.Data.EntityState.Modified; newoa.SaveChanges(); } } } } }
private void Button_Click_3(object sender, RoutedEventArgs e) { using (var ctx = new oaEntities()) { var T = ctx.T_FGJHtmlData.DefaultIfEmpty().ToList(); for (int i = 0; i < T.Count(); i++) { T_FGJHtmlData tf = T[i]; tf.SumMoneyID = GetMoney(tf.FwSumMoney); tf.HuXingID = GetHuxing(tf.FwHuXing); tf.MianjiID = GeiMinji(tf.FwMianji); tf.Money_int = GetMoney_int(tf.FwSumMoney); tf.Pingmi_int = GetPingmi_int(tf.FwMianji); } ctx.SaveChanges(); } }
public void Updata() { #region MyRegion using (var ctx = new oaEntities()) { var obj = (ctx as IObjectContextAdapter).ObjectContext; DateTime DBtime = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString()); MaxTime = ctx.T_FGJHtmlData.DefaultIfEmpty().Where(x => x.FbTime != DBtime).Max(x => x.FbTime); // var tfdata = ctx.T_FGJHtmlData.DefaultIfEmpty<T_FGJHtmlData>().Where(x=>x.FbTime==DBtime); iqdata = ctx.T_FGJHtmlData.DefaultIfEmpty <T_FGJHtmlData>().Where(x => x.FbTime == DBtime); web1.LoadCompleted += new LoadCompletedEventHandler(webbrowserUpload); loadingS = true; // 表示正在加载 web1.Navigate("http://liaoyang.58.com/ershoufang/0"); while (loadingS) { System.Windows.Forms.Application.DoEvents(); // 等待本次加载完毕才执行下次循环. } #region 查询完所有网站后执行 for (i = 0; i < L_Class.Count; i++) { web1.LoadCompleted += new LoadCompletedEventHandler(web2_Navigated);; loading = true; // 表示正在加载 web1.Navigate(L_Class[i].href); ChangeTextEvent("支目录条数已读取" + i + "条" + "-----主目录条数" + L_Class.Count()); while (loading) { System.Windows.Forms.Application.DoEvents(); // 等待本次加载完毕才执行下次循环. } } //DPdg.ItemsSource = L_Class; #endregion foreach (var da in L_Class) { T_FGJHtmlData tf = new T_FGJHtmlData(); //如果数据库中出现该名称并且 时间在当前时间那么 该信息不写入数据库 if (ctx.T_FGJHtmlData.FirstOrDefault(x => x.HLName == da.TextName) != null) { continue; } tf.HLName = da.TextName; tf.HLhref = da.href; tf.PersonName = da.PersonName; tf.Address = da.Address; tf.photo = da.photo; tf.FbTime = da.FbTime; tf.FwSumMoney = da.FwSumMoney; tf.FwHuXing = da.FwHuXing; tf.FwMianji = da.FwMianji; tf.FwLoucheng = da.FwLoucheng; tf.FwZhuangxiu = da.FwZhuangxiu; tf.FwNianxian = da.FwNianxian; tf.FwChaoxiang = da.FwChaoxiang; tf.bak = da.bak; tf.Id_count = int.Parse(da.Id_count == null ? "0" : da.Id_count); tf.Laiyuan = da.Laiyuan; tf.Image_str = da.Image_str; ctx.T_FGJHtmlData.Add(tf); } ctx.SaveChanges(); ChangeTextEvent("完成更新————" + DateTime.Now.ToString() + ".OK"); } #endregion this.Close(); }
private void NewHTMLhreperGJ() { string url = ""; this.Dispatcher.Invoke(new Action(() => { url = UPURLganji.Text; })); int cityItems = 0; this.Dispatcher.Invoke(new Action(() => { cityItems = Convert.ToInt32(ganjicombc.SelectedValue); })); try { using (var ctx = new oaEntities()) { #region 加载首页 DateTime DBtime = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString()); DateTime ds = DateTime.Now; var ftdef = ctx.T_FGJHtmlData.DefaultIfEmpty().Where(x => x.FbTime != DBtime && x.Laiyuan == "赶集" && x.CityID == cityItems && x.FbTime <= ds).FirstOrDefault(); if (ftdef != null) { MaxTime = ctx.T_FGJHtmlData.DefaultIfEmpty().Where(x => x.FbTime != DBtime && x.Laiyuan == "赶集" && x.CityID == cityItems && x.FbTime <= ds).Max(x => x.FbTime); } string ALLhtml = GetHTMLstr_gj(url, ""); ALLhtml = ALLhtml.Replace("<!-- ", string.Empty); ALLhtml = ALLhtml.Replace(" -->", string.Empty); IHtmlDocument document = new JumonyParser().Parse(ALLhtml); IEnumerable <IHtmlElement> result1 = document.Find(".list-items"); L_Class_Ganji.Clear(); foreach (var item in result1) { #region MyRegion try { newWORD.Class1 GJclass = new newWORD.Class1(); IHtmlElement item_a = item.FindFirst("a"); //信息名称 GJclass.TextName = item.FindFirst(".house-name").InnerText().Trim(); #region 判断时间的方法 string Timestr = item.FindFirst(".house-pulishtime").InnerText().Trim(); DateTime Dte = DateTime.Now; GJclass.FbTime = Convert.ToDateTime(Dte.Year.ToString() + "-" + Dte.Month.ToString() + "-" + Dte.Day.ToString()); Timestr = Timestr.Replace("更新于", string.Empty); Timestr = Timestr.Replace("前", string.Empty); if (Timestr != "今天") { if (Timestr.IndexOf("分钟") > -1) { if (Timestr.Replace("分钟", string.Empty).Trim().Length <= 0) { continue; } GJclass.FbTime = Dte.AddMinutes(-(Convert.ToInt32(Timestr.Replace("分钟", string.Empty)))); } else if (Timestr.IndexOf("小时") > -1) { if (Timestr.Replace("小时", string.Empty).Trim().Length <= 0) { continue; } GJclass.FbTime = Dte.AddHours(-(Convert.ToInt32(Timestr.Replace("小时", string.Empty)))); } else if (Timestr.IndexOf("天") > -1) { if (Timestr.Trim() == "昨天") { GJclass.FbTime = Dte.AddDays(-1); } else if (Timestr.Trim() == "前天") { GJclass.FbTime = Dte.AddDays(-2); } else { if (Timestr.Replace("天", string.Empty).Trim().Length <= 0) { continue; } GJclass.FbTime = Dte.AddDays(-3); } } else if (Timestr.Trim() == "刚刚") { GJclass.FbTime = DateTime.Now; } else { string[] strTime_ = Timestr.Split('-'); GJclass.FbTime = Convert.ToDateTime((GJclass.FbTime.Year - 1) + "-" + strTime_[0] + "-" + strTime_[1]); } if (GJclass.FbTime <= MaxTime) { continue; } } #endregion //连接 GJclass.href = item_a.Attribute("href").Value().IndexOf("https") > -1 ? item_a.Attribute("href").Value() : "https://3g.ganji.com" + item_a.Attribute("href").Value(); //地址 GJclass.Address = item.FindFirst(".house-addr").FindFirst(".house-area").InnerText(); L_Class_Ganji.Add(GJclass); } catch (Exception ex) { } #endregion } #endregion this.Dispatcher.Invoke(new Action(() => { GJSUMcount.Text = L_Class_Ganji.Count().ToString(); })); #region 加载分页 for (int i = 0; i < L_Class_Ganji.Count; i++) { Thread.Sleep(3000); try { this.Dispatcher.Invoke(new Action(() => { GJReadCount.Text = i.ToString(); })); string this_html = GetHTMLstr_gj(L_Class_Ganji[i].href, ""); if (this_html.Trim().Length <= 0) { continue; } IHtmlDocument document_this = new JumonyParser().Parse(this_html); //金额 L_Class_Ganji[i].FwSumMoney = document_this.Exists(".house-price") ? document_this.FindFirst(".house-price").InnerText().Replace("算房贷", string.Empty).Trim() : string.Empty; var this_Image = document_this.Find(".slide-area>li>img"); L_Class_Ganji[i].Image_str = string.Empty; foreach (var img in this_Image) { L_Class_Ganji[i].Image_str = L_Class_Ganji[i].Image_str.Length > 0 ? L_Class_Ganji[i].Image_str + "---" + img.Attribute("data-big-image").Value() : "有---" + img.Attribute("data-big-image").Value(); } var list = document_this.Find(".house-type>span"); //电话 var php = document_this.Find(".tel"); foreach (var item in php) { if (item.Attribute("href").Value().Trim().Length > 0) { L_Class_Ganji[i].photo = item.Attribute("href").Value().Trim().Replace("tel:", string.Empty); } } //联系人 L_Class_Ganji[i].Laiyuan = "赶集"; L_Class_Ganji[i].PersonName = document_this.Exists(".broker") ? document_this.FindFirst(".broker").FindFirst("span").InnerText().Replace("(个人)", string.Empty) : string.Empty; foreach (var item in list) { if (item.InnerText().IndexOf("朝") > -1) { L_Class_Ganji[i].FwChaoxiang = PFUANDtext(item, "朝"); } else if (item.InnerText().IndexOf("室") > -1) { L_Class_Ganji[i].FwHuXing = PFUANDtext(item, "室"); } else if (item.InnerText().IndexOf("层") > -1) { L_Class_Ganji[i].FwLoucheng = PFUANDtext(item, "层"); } else if (item.InnerText().IndexOf("㎡") > -1) { L_Class_Ganji[i].FwMianji = PFUANDtext(item, "㎡"); } else if (item.InnerText().IndexOf("产权") > -1) { L_Class_Ganji[i].FwNianxian = PFUANDtext(item, "产权"); } else if (item.InnerText().IndexOf("毛") > -1) { L_Class_Ganji[i].FwZhuangxiu = PFUANDtext(item, "毛"); } else if (item.InnerText().IndexOf("装修") > -1) { L_Class_Ganji[i].FwZhuangxiu = PFUANDtext(item, "装修"); } } } catch (Exception e) { } } #endregion #region 进行数据存储 SaveDataHTML(ctx, L_Class_Ganji, cityItems); ctx.SaveChanges(); #endregion } this.Dispatcher.Invoke(new Action(() => { GJLISTbox.Text = "完成更新————" + DateTime.Now.ToString(); })); } catch (Exception e) { this.Dispatcher.Invoke(new Action(() => { Ertext.Text = DateTime.Now.ToString() + e.ToString(); })); WhileBOOL2 = false; } WhileBOOL2 = false; }