示例#1
0
        public void FillVM(RouteInfoVM vm, HtmlNode nodeWithSummoryInfo, HtmlNode nodeWithDetails)
        {
            try
            {
                //получаем цену
                vm.Price =
                    nodeWithSummoryInfo.Descendants()
                    .Single(n => n.GetAttributeValue("class", "").Equals("mainquote-group-price"))
                    .Descendants("a")
                    .First()
                    .InnerText.Replace(" ", " ");
                // парсим секции
                var sections = nodeWithSummoryInfo.Descendants("section").ToList();

                //заполняем маршруты именами авиокомпаний, датами
                FillSummoryRouteModel(sections.First(), vm.Routes[0]);
                FillSummoryRouteModel(sections.Last(), vm.Routes[1]);


                var routeDetailsHtml =
                    nodeWithDetails.Descendants()
                    .Where(n => n.GetAttributeValue("class", "").Equals("itinerary-leg  "))
                    .ToList();
                //заполняем маршруты номера маршрутами
                FillDetailsRouteModel(routeDetailsHtml.First(), vm.Routes[0]);
                FillDetailsRouteModel(routeDetailsHtml.Last(), vm.Routes[1]);
            }
            catch (Exception)
            {
                throw new Exception("Не удалось распарсить html элементы");
            }
        }
示例#2
0
 public FillVMServiceTests()
 {
     fillVmService = new FillVMService();
     routeInfoVm   = new RouteInfoVM {
         Routes = new List <RouteInfo>()
         {
             new RouteInfo(), new RouteInfo()
         }
     };
     htmlSummoryDoc = new HtmlDocument();
     htmlDetailsDoc = new HtmlDocument();
 }
        public RouteInfoVM ExtractRouteInfo(SearchRouteParameters parameters)
        {
            var result = new RouteInfoVM {
                Routes = new List <RouteInfo> {
                    new RouteInfo(), new RouteInfo()
                }
            };

            var htmlSummoryDoc = new HtmlDocument();
            var htmlDetailsDoc = new HtmlDocument();

            var url =
                $"https://www.skyscanner.ru/transport/flights/{parameters.Source.ToLower()}/{parameters.Destination.ToLower()}/{parameters.DateSource}/{parameters.DateDestination}#results";

            loadPageForGetRouteHtmlElementsService.WaitLoadPage(url);
            htmlSummoryDoc.LoadHtml(loadPageForGetRouteHtmlElementsService.GetSummoryInnerHtml());
            htmlDetailsDoc.LoadHtml(loadPageForGetRouteHtmlElementsService.GetDetailsInnerHtml());
            loadPageForGetRouteHtmlElementsService.Dispose();

            //начинаем парсить и заполнять вьюмодель данными
            fillVmService.FillVM(result, htmlSummoryDoc.DocumentNode, htmlDetailsDoc.DocumentNode);

            return(result);
        }