Ejemplo n.º 1
0
        public async Task <List <ProductModel> > Search(string input)
        {
            var resultList = new List <ProductModel>();

            try
            {
                HttpClient client   = new HttpClient();
                var        response = await client.GetAsync("https://www.ferguson.com/category?Ntt=" + input + "&searchKeyWord=" + input);

                var pageContents = await response.Content.ReadAsStringAsync();

                HtmlDocument pageDocument = new HtmlDocument();
                pageDocument.LoadHtml(pageContents);

                var itemCount = _xPathService.GetElementCount("(//div[contains(@class, 'prod-name')]//p)", pageDocument);

                for (var i = 1; i <= itemCount; i++)
                {
                    var model = new ProductModel();

                    model.Company = "Ferguson";
                    model.Name    = _xPathService.GetElementText("(//div[contains(@class, 'prod-name')]//p)[" + i + "]", pageDocument).Replace("&amp174;", string.Empty) ?? string.Empty;
                    model.Price   = _xPathService.GetElementText("(//p[contains(@class, 'price')])[" + i + "]", pageDocument) ?? string.Empty;

                    resultList.Add(model);
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }

            return(resultList);
        }
Ejemplo n.º 2
0
        public async Task <List <ProductModel> > Search(string input)
        {
            var resultList = new List <ProductModel>();

            try
            {
                HttpClient client = new HttpClient(new HttpClientHandler
                {
                    AllowAutoRedirect = false,
                });

                var response = await client.GetAsync("https://www.supplyhouse.com/sh/control/search/~SEARCH_STRING=" + input + "?searchText=" + input);

                var pageContents = await response.Content.ReadAsStringAsync();

                HtmlDocument pageDocument = new HtmlDocument();
                pageDocument.LoadHtml(pageContents);

                var itemCount = _xPathService.GetElementCount("(//div[contains(@class, 'desc')])", pageDocument);

                for (var i = 1; i <= itemCount; i++)
                {
                    var model = new ProductModel();

                    model.Company = "Supply House";
                    model.Name    = _xPathService.GetElementText("(//div[(@class, 'desc')]//a)[1]", pageDocument);
                    model.Price   = _xPathService.GetElementText("(//div[(@class, 'unit-price')]//span)[1]", pageDocument);

                    resultList.Add(model);
                }
            }

            catch (Exception ex)
            {
                throw ex;
            }

            return(resultList);
        }