Exemplo n.º 1
0
        //Метод парсер HTML
        public void Parser(string document, ref int countProduct)
        {
            string                price           = null;
            string                nameProduct     = null;
            string                description     = null;
            string                id              = null;
            List <string>         listPhoto       = null;
            string                sourse          = null;
            List <ModelDatePrice> modelDatePrices = null;
            Product               product         = null;
            HtmlParser            htmlParser      = new HtmlParser();
            IHtmlDocument         htmlDocument    = htmlParser.Parse(document);

            try
            {
                var elements = htmlDocument.GetElementsByClassName("plate-box").ToList();
                for (int item = 0; item < elements.Count; item++)
                {
                    if (countProduct >= 135)
                    {
                        return;
                    }
                    id = elements[item].GetElementsByClassName("id")
                         .ToList()[0].QuerySelector("span").TextContent;
                    if (ManagerShope.listProduct.Exists(e => e.id == id))
                    {
                        continue;
                    }
                    modelDatePrices = new List <ModelDatePrice>();
                    listPhoto       = new List <string>();
                    IHtmlDocument document1 = null;
                    var           el        = elements[item].QuerySelectorAll("div")
                                              .Where(elem => elem.ClassName == "title")
                                              .ToList()[0].QuerySelector("a");
                    string url = el.OuterHtml.Remove(0, el.OuterHtml.IndexOf('\"') + 2);

                    url              = urlShope + "/" + url.Remove(url.IndexOf('>') - 1);
                    sourse           = ConnectorShope.GetContentSimplePage(url).GetAwaiter().GetResult();
                    document1        = htmlParser.Parse(sourse);
                    nameProduct      = GetNameProductProduct(el);
                    price            = GetPriceProduct(elements[item]);
                    description      = GetNameDescriptionProduct(elements[item]);
                    listPhoto        = GetNameListPhotoProduct(document1);
                    product          = new Product(price, description, listPhoto, nameProduct, id);
                    product.dataTime = GetDatePrices(price);
                    ManagerShope.listProduct.Add(product);

                    countProduct++;
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }
Exemplo n.º 2
0
        //Метод частично парсит HTML
        public void PartialParser(string document, ref int countProduct, List <ModelProductDAO> modelProductDAO)
        {
            string        price        = null;
            string        nameProduct  = null;
            string        description  = null;
            string        id           = null;
            List <string> listPhoto    = null;
            string        sourse       = null;
            Product       product      = null;
            HtmlParser    htmlParser   = new HtmlParser();
            IHtmlDocument htmlDocument = htmlParser.Parse(document);

            try
            {
                var elements = htmlDocument.GetElementsByClassName("plate-box").ToList();
                for (int item = 0; item < elements.Count; item++)
                {
                    if (countProduct >= 135)
                    {
                        return;
                    }
                    id = elements[item].GetElementsByClassName("id").ToList()[0].QuerySelector("span").TextContent;
                    var currProd = modelProductDAO.Find(p => p.id == id);
                    if (currProd != null)
                    {
                        price     = GetPriceProduct(elements[item]);
                        listPhoto = new List <string>();
                        foreach (var photo in currProd.listPhoto)
                        {
                            listPhoto.Add(photo.photo);
                        }
                        description      = currProd.description;
                        nameProduct      = currProd.nameProduct;
                        product          = new Product(price, description, listPhoto, nameProduct, id);
                        product.dataTime = GetDatePrices(price, currProd);
                        ManagerShope.listProduct.Add(product);
                    }
                    else
                    {
                        IHtmlDocument document1 = null;
                        var           el        = elements[item].QuerySelectorAll("div")
                                                  .Where(elem => elem.ClassName == "title")
                                                  .ToList()[0].QuerySelector("a");
                        string url = el.OuterHtml.Remove(0, el.OuterHtml.IndexOf('\"') + 2);

                        url              = el.BaseUrl + url.Remove(url.IndexOf('>') - 1);
                        sourse           = ConnectorShope.GetContentSimplePage(url).GetAwaiter().GetResult();
                        document1        = htmlParser.Parse(sourse);
                        nameProduct      = GetNameProductProduct(el);
                        price            = GetPriceProduct(elements[item]);
                        description      = GetNameDescriptionProduct(elements[item]);
                        listPhoto        = GetNameListPhotoProduct(document1);
                        product          = new Product(price, description, listPhoto, nameProduct, id);
                        product.dataTime = GetDatePrices(price);
                        ManagerShope.listProduct.Add(product);
                    }

                    countProduct++;
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message);
            }
        }