예제 #1
0
        private void NewHTMLhreper58()
        {

           
                string url = "";
                this.Dispatcher.Invoke(new Action(() =>
                {
                    url = up58nameurl.Text;

                }));

                string ALLhtml = GetHTMLstr(url);
                int cityItems = 0;
                this.Dispatcher.Invoke(new Action(() =>
                {
                    cityItems = Convert.ToInt32(up58city.SelectedValue);
                }));
                using (var ctx = new oaEntities())
                {
                    DateTime DBtime = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString());
                    var QZQtime = ctx.T_ChuZhuInfo.Count();
                    if (QZQtime <= 0)
                    {

                    }
                    var timesdata = ctx.T_ChuZhuInfo.Where(x => x.LaiYuan == "58").DefaultIfEmpty();
                    var MaxTime = timesdata.First()!=null?(DateTime) timesdata.Max(x => x.FbTime) : new DateTime(2016, 12, 12);

                   // MaxTime = QZQtime <= 0 ? new DateTime(2016, 12, 12) : times ;
                    #region 开始读取网站列表信息
                    IHtmlDocument document = new JumonyParser().Parse(ALLhtml);
                    IEnumerable<IHtmlElement> result = document.Find(".listBox");
                    IEnumerable<IHtmlElement> t = result.Find("li");
                    DateTime Ttdtime = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-1");
                    var TTimeData = ctx.T_ChuZhuInfo.Where(x => x.FbTime >= Ttdtime).DefaultIfEmpty();
                    //Dictionary<string, string> dir = new Dictionary<string, string>();                  
                    foreach (var item in t)
                    {
                      //  DateTime uptime = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString());
                     //   DateTime NOtime = DateTime.Now;
                      //  var drtme = ctx.T_FGJHtmlData.DefaultIfEmpty().Where(x => x.FbTime != uptime && x.Laiyuan == "58" && x.CityID == cityItems && x.FbTime <= NOtime).FirstOrDefault();
                      //  MaxTime = drtme == null ? uptime.AddDays(-1) : ctx.T_FGJHtmlData.DefaultIfEmpty().Where(x => x.FbTime != uptime && x.Laiyuan == "58" && x.CityID == cityItems && x.FbTime <= NOtime).Max(x => x.FbTime);
                        #region 开始读取网站列表信息
                       // IHtmlDocument document58 = new JumonyParser().Parse(ALLhtml);
                       // IEnumerable<IHtmlElement> result58 = document58.Find(".house-list-wrap");
                      //  IEnumerable<IHtmlElement> t58 = result.Find("li");
                        Dictionary<string, string> dir = new Dictionary<string, string>();
                        int ret = 0;
                        #region MyRegion
                        newWORD.Class1 _class = new newWORD.Class1();

                        IEnumerable<IHtmlElement> sendTime = item.Find(".sendTime");
                        string Timestr="";
                        foreach (var h1 in sendTime)
                        {
                            Timestr = h1.InnerText();
                        }
                        DateTime Dte = DateTime.Now;
                        _class.FbTime = Convert.ToDateTime(Dte.Year.ToString() + "-" + Dte.Month.ToString() + "-" + Dte.Day.ToString());
                        string timerss = Timestr.Replace(" ", "");
                        if (timerss.Length > 0)
                        {
                            Timestr = Timestr.Replace("更新于", string.Empty);
                            Timestr = Timestr.Replace("前", string.Empty);
                            if (Timestr != "今天")
                            {
                                #region MyRegion
                                if (Timestr.IndexOf("分钟") > -1)
                                {
                                    ret = IFthis_I(ret);
                                    _class.FbTime = Dte.AddMinutes(-(Convert.ToInt32(Timestr.Replace("分钟", string.Empty))));
                                }
                                else if (Timestr.IndexOf("小时") > -1)
                                {
                                    ret = IFthis_I(ret);
                                    _class.FbTime = Dte.AddHours(-(Convert.ToInt32(Timestr.Replace("小时", string.Empty))));
                                }
                                else if (Timestr.IndexOf("天") > -1)
                                {
                                    if (Timestr.Trim() == "昨天")
                                    { _class.FbTime = Dte.AddDays(-1); }
                                    else
                                    {
                                        if (Timestr.Replace("天", string.Empty).Trim().Length <= 0)
                                        { continue; }
                                        _class.FbTime = Dte.AddDays(-3);
                                    }
                                }
                                else
                                {
                                    string[] strTime = Timestr.Split('-');
                                    _class.FbTime = Convert.ToDateTime((_class.FbTime.Year - 1) + "-" + strTime[0] + "-" + strTime[1]);
                                }

                                if (_class.FbTime <= MaxTime)
                                {

                                    if (ret >= 2)
                                    {
                                        break;
                                    }
                                    ret++;
                                }
                                else
                                {
                                    ret = 0;
                                }
                                #endregion
                            }
                        }
                        else {
                            _class.FbTime = _class.FbTime.AddDays(-1);
                        }


                        IEnumerable<IHtmlElement> hname = item.Find("h2");
                        foreach (var h1 in hname)
                        {
                           _class.TextName= h1.InnerText();
                        }
                    if (_class.TextName == null)
                    {
                        continue;
                    }
                        IEnumerable<IHtmlElement> tingshi = item.Find(".room");
                        foreach (var h1 in tingshi)
                        {
                            _class.FwHuXing = h1.InnerText();
                        }
                        string pinx = _class.FwHuXing;
                       
                        pinx = pinx.Remove(0, pinx.IndexOf("卫") + 1).Replace("㎡", "").Replace(" ","");
                        _class.FwHuXing = _class.FwHuXing.Replace(pinx,"");

                        _class.FwMianji = pinx.Remove(0, pinx.IndexOf("  ") + 1).Replace("  ", "");
                        IEnumerable<IHtmlElement> addess = item.Find(".add");
                        foreach (var h1 in addess)
                        {
                            _class.Address = h1.InnerText();
                        }

                        IEnumerable<IHtmlElement> person = item.Find(".geren");
                        foreach (var h1 in person)
                        {
                            _class.PersonName = h1.InnerText();
                        }
                        _class.PersonName = _class.PersonName.Replace("来自个人房源:", "");
                        IEnumerable<IHtmlElement> images = item.Find("img");
                        foreach (var h1 in images)
                        {
                            _class.Image_str= "---" + h1.Attribute("lazy_src").Value();                          
                        }

                        IEnumerable<IHtmlElement> money = item.Find(".money>b");
                        foreach (var h1 in money)
                        {
                            _class.SumMoney = h1.InnerText();
                        }
                        if (item.Exists("h2 > a")) {
                            IEnumerable<IHtmlElement> href = item.Find("h2>a");
                            foreach (var h1 in href)
                            {
                                _class.href = h1.Attribute("href").Value();
                            }
                        }
                        _class.Laiyuan = "58";
                        r++;
                        #endregion
                        T_ChuZhuInfo tqzq = new T_ChuZhuInfo();
                        tqzq.ChuZhuName = _class.TextName;
                        tqzq.Addess = _class.Address;
                        int tmp;
                        if (!int.TryParse(_class.SumMoney, out tmp))
                        {
                            _class.SumMoney = "0";
                        }

                        tqzq.Money =  Convert.ToDecimal(_class.SumMoney);
                        tqzq.LaiYuan = _class.Laiyuan;
                        tqzq.Images = _class.Image_str;
                        tqzq.FbTime = _class.FbTime;
                        tqzq.PingMi = Convert.ToDecimal(_class.FwMianji.Replace(" ", ""));
                        tqzq.LianXiPerson = _class.PersonName;
                        tqzq.LianXiPhoto = _class.photo;
                        tqzq.Bak = _class.bak;
                        tqzq.ChuZhuHref = _class.href;
                        tqzq.CityID = cityItems;
                        var distic= TTimeData.Where(x => x.ChuZhuName == tqzq.ChuZhuName && x.Addess == tqzq.Addess && x.PingMi == tqzq.PingMi && x.Money == tqzq.Money).FirstOrDefault();
                        if (distic == null) {
                            ctx.T_ChuZhuInfo.Add(tqzq);
                            ctx.SaveChanges();
                        }
                        

                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            ADDC58.Text = r.ToString();

                        }));
                      
                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            ltextd58.Text = "更新一条数据————" + _class.TextName + "———" + DateTime.Now.ToString();

                        }));
                        #endregion

                    }
                    #endregion
                }
            WhileBOOL58 = false;
            try
            {
            }
            catch (Exception ex)
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    LISTbox.Text = DateTime.Now.ToString() + ex.ToString();

                }));

            }
          
        }
예제 #2
0
        private void CJTXNewHTMLhreper()
        {
            
            try
            {
                string url = "";
                this.Dispatcher.Invoke(new Action(() =>
                {
                    url = FTXtexturl.Text;

                }));
                string ALLhtml = BXGetHTMLstr(url);
                using (var ctx = new oaEntities())
                {
                    //获取当前时间
                    DateTime DBtime = Convert.ToDateTime(DateTime.Now.Year.ToString() + "-" + DateTime.Now.Month.ToString() + "-" + DateTime.Now.Day.ToString());
                    var QZQtime = ctx.T_ChuZhuInfo.Count();
                    if (QZQtime <= 0)
                    {

                    }
                    MaxTime = QZQtime <= 0 ? new DateTime(2016, 12, 12) : Convert.ToDateTime(ctx.T_ChuZhuInfo.DefaultIfEmpty().Max(x => x.FbTime));

                    #region 开始读取网站列表信息
                    IHtmlDocument document = new JumonyParser().Parse(ALLhtml);
                    IEnumerable<IHtmlElement> result = document.Find(".hpic");

                    foreach (var item in result)
                    {

                        #region MyRegion
                        Class1 _class = new Class1();

                        _class.href = item.Exists(".pic_box") ? item.FindFirst(".pic_box").Attribute("href").Value() : string.Empty;
                        _class.TextName = item.Exists(".pic_box") ? item.FindFirst(".pic_box").Attribute("title").Value() : string.Empty;
                        

                        if (QZQtime > 0)
                        {
                            var listc = ctx.T_ChuZhuInfo.Where(u => u.ChuZhuName == _class.TextName && u.ChuZhuHref == _class.href);
                            if (listc.Count() >= 1)
                            {
                                this.Dispatcher.Invoke(new Action(() =>
                                {
                                    ltextd.Text = "重复信息退出更新等待下一次更新———" + _class.TextName + "———" + DateTime.Now.ToString();

                                }));
                                CJTXWhileBOOL1 = false;
                                return;
                            }

                        }

                        _class.Address = MainWindow.GetN_value(item, ".addr");
                        _class.SumMoney = MainWindow.GetN_value(item, ".pr");
                        _class.Laiyuan = "cjfl";

                        #region 进入子页面
                        string html = GetHTMLstr(_class.href);
                        IHtmlDocument document_1 = new JumonyParser().Parse(html);
                        IEnumerable<IHtmlElement> result1 = document_1.Find(".viewad_box>.viewad-meta > li");
                        IEnumerable<IHtmlElement> result2 = document_1.Find(".view_box>p");
                        IEnumerable<IHtmlElement> result3 = document_1.Find(".viewad-actions");
                        IEnumerable<IHtmlElement> image = document_1.Find(".container>.pic_box");

                        int i = 0;
                        foreach (var tm in result1)
                        {
                            if (i == 2)
                            {
                                _class.Mianji = MainWindow.GetN_value(tm, "span");
                            }
                            if (i == 4)
                            {
                                _class.PersonName = MainWindow.GetN_value(tm, "span");
                            }
                            if (i == 5)
                            {
                                _class.photo = MainWindow.GetN_value(tm, "span");
                            }
                            i++;

                        }
                        i = 0;
                        foreach (var ts in result2)
                        {
                            if (i == 1)
                            { _class.bak = ts.InnerHtml(); }
                            i++;

                        }
                        string imagerui = "";
                        foreach (var tm in image)
                        {
                            imagerui = imagerui + "---" + tm.FindFirst("img ").Attribute("src").Value();

                        }
                        _class.Image_str = imagerui;
                        foreach (var ts in result3)
                        {
                            _class.FbTime = Convert.ToDateTime(ts.FindFirst("span").InnerHtml());

                        }

                        #endregion

                        #endregion

                        #region 写入系统

                        if (item.Exists("a"))
                        {
                            T_ChuZhuInfo tqzq = new T_ChuZhuInfo();
                            tqzq.ChuZhuName = _class.TextName;
                            tqzq.Addess = _class.Address;
                            tqzq.Money = Convert.ToDecimal(_class.SumMoney);
                            tqzq.LaiYuan = _class.Laiyuan;
                            tqzq.Images = _class.Image_str;
                            tqzq.FbTime = _class.FbTime;
                            tqzq.PingMi = Convert.ToDecimal(_class.Mianji.Replace("㎡", ""));
                            tqzq.LianXiPerson = _class.PersonName;
                            tqzq.LianXiPhoto = _class.photo;
                            tqzq.Bak = _class.bak;
                            tqzq.ChuZhuHref = _class.href;

                            this.Dispatcher.Invoke(new Action(() =>
                            {
                                tqzq.CityID = Convert.ToInt32(ftxCombx.SelectedValue);

                            }));
                            ctx.T_ChuZhuInfo.Add(tqzq);
                            //  L_Class.Add(_class);
                            r++;
                            this.Dispatcher.Invoke(new Action(() =>
                            {
                                ADDC.Text = r.ToString();

                            }));
                            ctx.SaveChanges();
                        }

                        #endregion

                        this.Dispatcher.Invoke(new Action(() =>
                        {
                            ltextd.Text = "更新一条数据————" + _class.TextName + "———" + DateTime.Now.ToString();

                        }));
                    }
                    #endregion
                }

            }
            catch (Exception ex)
            {
                this.Dispatcher.Invoke(new Action(() =>
                {
                    ltextd.Text = DateTime.Now.ToString() + ex.ToString();

                }));

            }
            WhileBOOL1 = false;
        }