public void ParseFrom(string url) { ParseFrom(url, 1, MarketItems.PageCount(url)); }
public static void Export(string filename, MarketItems item) { MarketItems items = new MarketItems(); foreach (var a in item) { items.Add(a); if (a.ChildrenItems.Count != 0) { foreach (var b in a.ChildrenItems) { items.Add(b); } } } using (ExcelPackage xlPackage = new ExcelPackage(new FileInfo(filename))) { var Worksheet = xlPackage.Workbook.Worksheets.First(); //select sheet here var totalRows = Worksheet.Dimension.End.Row; var totalColumns = Worksheet.Dimension.End.Column; Dictionary <string, List <string> > Colls = new Dictionary <string, List <string> >(); for (int col = 1; col <= totalColumns; col++) { if (Worksheet.Cells[1, col].Value.ToString() == "*Model") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Model; } } if (Worksheet.Cells[1, col].Value.ToString() == "*Name") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Name; } } if (Worksheet.Cells[1, col].Value.ToString() == "Description") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Description; } } if (Worksheet.Cells[1, col].Value.ToString() == "Meta title") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Meta_title; } } if (Worksheet.Cells[1, col].Value.ToString() == "SEO url") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].SEO_url; } } if (Worksheet.Cells[1, col].Value.ToString() == "Quantity") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Quantity; } } if (Worksheet.Cells[1, col].Value.ToString() == "Out stock status") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Out_stock_status; } } if (Worksheet.Cells[1, col].Value.ToString() == "Option") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Option; } } if (Worksheet.Cells[1, col].Value.ToString() == "Option value") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Option_value; } } if (Worksheet.Cells[1, col].Value.ToString() == "Price") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Price; } } if (Worksheet.Cells[1, col].Value.ToString() == "Manufacturer") { for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = items[i].Manufacturer; } } if (Worksheet.Cells[1, col].Value.ToString() == "Main image") {// "catalog/tovar/" + a.Model+".jpg" for (int i = 0; i < items.Count; i++) { Worksheet.Cells[i + 2, col, i + 2, col].Value = "catalog/tovar/" + items[i].Model + ".jpg"; } } xlPackage.Save(); } } }
public void ParseFrom(string url, int StartPage, int FinishPage) { try { var doc = new HtmlWeb().Load(url + "/goods.php"); // int pageCount = MarketItems.PageCount(url); //проверка по пагинации for (int page = StartPage; page <= FinishPage; page++) { doc = new HtmlWeb().Load(url + "/goods.php?cid=5&page=" + page); //текущая страница var pageGoods = doc.DocumentNode.SelectNodes("//div[@class='ernr']/ul/li/div/h3/a"). Select(a => url + "/" + a.ChildAttributes("href"). FirstOrDefault().Value); //ссылки на все товары на странице if (pageGoods.Count() == 0) { throw new Exception("Товары не найдены!\nВозможно, сайт поменял верстку. Требуется обновить ПО!"); } else { foreach (var good in pageGoods.ToList()) { MarketItem item = new MarketItem();//будем заполнять товар try { var goodNode = new HtmlWeb().Load(good).DocumentNode.SelectSingleNode("//div[@class='cps']"); //html товара. отсюда и вытащим всё var Options = MarketItems.ConvertList(goodNode.SelectNodes(".//div[@class='tabmen']/ul/li").Select(a => a.InnerText).ToList()); var Quantities = ConvertList(goodNode.SelectNodes(".//div[@id='tabconten']/ul/li").Select(a => a.InnerText.Replace("双", "")).ToList()); // item.Model = Count != 0 ? this.Max(a => a.Model) + 1 : 1; item.Name = goodNode.SelectSingleNode(".//h6").InnerText + " " + item.Model; item.Description = "<p><br></p>"; item.SEO_url = item.Name.Replace(" ", "-"); item.Out_stock_status = ""; item.Option_type = "radio"; item.Price = "";//сам вводит item.Main_image = url + "/" + goodNode.SelectSingleNode(".//img").ChildAttributes("src").FirstOrDefault().Value; /*TODO*/ item.Manufacturer = Manuf(item.Name); item.Option = MarketItems.Option(Options); string resName = ""; if (item.Manufacturer.Contains("Timber")) { resName += "Ботинки"; } else { resName += "Кроссовки"; } if (Options.Min() <= 36) { resName += " Женские "; } else { resName += " Мужские "; } item.Name = resName + goodNode.SelectSingleNode(".//h6").InnerText + " " + item.Model; item.Meta_title = item.Name; //по парам item.Quantity = Quantities.First().ToString(); item.Option_value = Options.First().ToString(); if (Options.Count == Quantities.Count) { if (Options.Count > 1) { for (int i = 1; i < Options.Count; i++) { MarketItem child = new MarketItem(); child.Model = item.Model; child.Name = item.Name; child.Description = ""; child.Meta_title = ""; child.SEO_url = ""; child.Option = item.Option; child.Option_type = item.Option_type; child.Main_image = ""; child.Quantity = Quantities[i].ToString(); child.Option_value = Options[i].ToString(); item.ChildrenItems.Add(child); } } } else { throw new Exception(); } } catch (Exception exception) { item.Error = exception.Message.ToString(); } Add(item);//после парсинга страницы добавляем элемент } } } } catch (Exception e) { throw new Exception("Произошла ошибка при попытке получить страницу по адресу: " + url + "\n\t\t" + e.Message + "\nПожалуйста, проверьте правильность ввода ресурса!"); } }
public static MarketItems Import(string filename) { using (ExcelPackage xlPackage = new ExcelPackage(new FileInfo(filename))) { var Worksheet = xlPackage.Workbook.Worksheets.First(); //select sheet here var totalRows = Worksheet.Dimension.End.Row; var totalColumns = Worksheet.Dimension.End.Column; // var sb = new StringBuilder(); //this is your your data Dictionary <string, List <string> > Colls = new Dictionary <string, List <string> >(); for (int col = 1; col <= totalColumns; col++) { var Column = Worksheet.Cells[1, col, totalRows, col]. Select(x => x.Value == null ? " " : x.Value.ToString()).ToList(); Colls.Add(Column.First(), Column); } MarketItems result = new MarketItems(); for (int i = 1; i < Colls["*Model"].Count - 1; i++) { try { if ((Colls["*Model"][i] == Colls["*Model"][i - 1]) && (result.Last() != null)) { result.Last().ChildrenItems.Add(new MarketItem { Model = Convert.ToInt32(Colls["*Model"][i]), Name = Colls["*Name"][i], Description = result.Last().Description, Meta_title = result.Last().Meta_title, SEO_url = result.Last().SEO_url, Quantity = Colls["Quantity"][i], Out_stock_status = result.Last().Out_stock_status, Option = result.Last().Option, Option_value = Colls["Option value"][i], Price = Colls["Price"][i], Manufacturer = result.Last().Manufacturer, Main_image = result.Last().Main_image }); } else { result.Add(new MarketItem { Model = Convert.ToInt32(Colls["*Model"][i]), Name = Colls["*Name"][i], Description = Colls["Description"][i], Meta_title = Colls["Meta title"][i], SEO_url = Colls["SEO url"][i], Quantity = Colls["Quantity"][i], Out_stock_status = Colls["Out stock status"][i], Option = Colls["Option"][i], Option_value = Colls["Option value"][i], Price = Colls["Price"][i], Manufacturer = Colls["Manufacturer"][i], Main_image = Colls["Main image"][i] }); } } catch { result.Add(new MarketItem()); } } return(result); } }