예제 #1
0
        List <AreaModel> GetCitiesUrl(string url, int parentId = 0)
        {
            //var result = String.Empty;
            List <AreaModel> lstCities = new List <AreaModel>();

            try
            {
                var resultHtml = GetContent(url, ".thongtincongty.com");
                //using Html Agility Pack
                var doc = new HtmlAgilityPack.HtmlDocument();
                doc.LoadHtml(resultHtml);
                // Get city list url
                var cityXPath = "//*[@id='sidebar']/div[@class='list-group']/a";
                CopyLogger.Debug("\ncityXPath:" + cityXPath);
                // Get news by area categories
                int cityId = 1;
                foreach (HtmlNode link in doc.DocumentNode.SelectNodes(cityXPath))
                {
                    int total   = link.LinePosition;
                    int percent = (cityId / total) * 100;
                    buildCopyWorker.ReportProgress(percent);
                    if (link.InnerText.TrimStart().TrimEnd() == "Toàn Quốc")
                    {
                        continue;
                    }
                    Thread.Sleep(250);
                    var model = new AreaModel();
                    model.AreaId = cityId;
                    var cityName = link.InnerText.TrimStart().TrimEnd();
                    var cityUrl  = link.Attributes["href"].Value.ToString();
                    model.AreaName = cityName;
                    model.AreaUrl  = cityUrl;
                    model.ParentId = parentId;
                    CopyLogger.Debug(model.ToString());
                    model.InsertAreaData(model);
                    lstCities.Add(model);
                    cityId++;
                }
            }
            catch (Exception ex)
            {
                CopyLogger.Error(string.Format("Trace Error:{0} \n Error Message:{1}",
                                               ex.ToString(), ex.Message));
            }
            return(lstCities);
        }