public RedirectToRouteResult CreateParser(ParserCreator model) { var parser = new ParseInfo { MarketId = model.MarketId, ParseLink = model.ParseLink, ProductNameXpath = model.ProductNameXpath, PriceXpath = model.PriceXpath, PagingXpath = model.PagingXpath, IsActive = true }; context.ParseInfoes.Add(parser); context.SaveChanges(); TempData["create"] = "Success"; return RedirectToAction("Index"); }
public RedirectToRouteResult EditParser(ParseInfo model) { var parser = context.ParseInfoes.FirstOrDefault(x => x.Id == model.Id); string message; if (parser != null) { parser.MarketId = model.MarketId; parser.ParseLink = model.ParseLink; parser.ProductNameXpath = model.ProductNameXpath; parser.PriceXpath = model.PriceXpath; parser.PagingXpath = model.PagingXpath; context.SaveChanges(); message = "Success"; } else { message = "Failed"; } TempData["edit"] = message; return RedirectToAction("Index"); }
public ActionResult SetActive(int id) { var parser = context.ParseInfoes.FirstOrDefault(x => x.Id == id); bool statusFlag = false; if (ModelState.IsValid) { if (parser.IsActive == true) { parser.IsActive = false; statusFlag = false; } else { parser.IsActive = true; statusFlag = true; } context.SaveChanges(); } // Display the confirmation message var results = new ParseInfo { IsActive = statusFlag }; return Json(results); }
private static List<KeyValuePair<string, string>> GetData(HtmlWeb web, ParseInfo info) { var data = new List<KeyValuePair<string, string>>(); Uri uri = new Uri(info.ParseLink); string host = uri.GetLeftPart(UriPartial.Authority); // Load website try { // First page HtmlDocument doc = web.Load(info.ParseLink); data = MatchData(doc, info.ProductNameXpath, info.PriceXpath); // Other pages if (info.PagingXpath != null) { var pages = doc.DocumentNode.SelectNodes(info.PagingXpath); foreach (var page in pages) { int pageNumber; if (page == null || !Int32.TryParse(page.InnerText, out pageNumber)) { break; } // Get address string url = host + page.Attributes["href"].Value; // Load page doc = web.Load(url); // Get data var tmp = MatchData(doc, info.ProductNameXpath, info.PriceXpath); // Add to collection data.AddRange(tmp); } } } catch (Exception) { return data; } return data; }