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; }