/// <summary> /// Generates scannable URLs for the given searchterm /// </summary> /// <param name="searchterm">Action Movie</param> /// <returns>A list of ShopItems matching the searchterm</returns> public List<ShopItem> GetListOfSearchResults(string searchterm) { List<ShopItem> resultList = new List<ShopItem>(); HtmlWeb web = new HtmlWeb(); IEnumerable<HtmlNode> nextPageElements; // URL for the current page iteration string currentURL; int currentPage = 1; HtmlNode titleNode; HtmlNode priceNode; HtmlNode dateNode; ShopItem shopItem; string title = "n.a."; string price = "n.a."; string date = "n.a."; string url = "n.a."; string platform = "Ebay Kleinanzeigen"; do { currentURL = GetUrlEbayKleinanzeigen(searchterm, currentPage); HtmlDocument document = web.Load(currentURL); HtmlNode page = document.DocumentNode; IEnumerable<HtmlNode> searchResults = page.Descendants().Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("ad-listitem ")); // Each node represents one search result foreach (HtmlNode searchResult in searchResults) { // Extract title and price, add them to the DataGrid titleNode = searchResult.SelectSingleNode("./section[@class='ad-listitem-main']/h2/a"); priceNode = searchResult.SelectSingleNode("./section[@class='ad-listitem-details']/strong"); dateNode = searchResult.SelectSingleNode("./section[@class='ad-listitem-addon']"); if (titleNode != null) { title = titleNode.InnerText.Trim(); url = "http://kleinanzeigen.ebay.de/" + titleNode.Attributes["href"].Value.Trim(); } if (priceNode != null) { price = priceNode.InnerText.Trim(); } if (dateNode != null) { date = dateNode.InnerText.Trim(); } shopItem = new ShopItem { Title = title, Price = price, Date = date, URL = url, Platform = platform }; resultList.Add(shopItem); } // Extract Next Page Elements nextPageElements = page.Descendants().Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("pagination-btn icon-paging-next")); currentPage++; } // Is there a Next Page button? while (nextPageElements.Count() > 0); this._numberOfLastResults = --currentPage; return resultList; }
/// <summary> /// Generates scannable URLs for the given searchterm /// </summary> /// <param name="searchterm">Action Movie</param> /// <returns>A list of ShopItems matching the searchterm</returns> public List<ShopItem> GetListOfSearchResults(string searchterm) { List<ShopItem> resultList = new List<ShopItem>(); HtmlWeb web = new HtmlWeb(); IEnumerable<HtmlNode> nextPageElements; // URL for the current page iteration string currentURL; int currentPage = 1; HtmlNode titleNode; HtmlNode priceNode; HtmlNode urlNode; ShopItem shopItem; string title = "n.a."; string price = "n.a."; string date = "n.a."; string url = "n.a."; string platform = "Ebay"; do { currentURL = GetUrlEbay(searchterm, currentPage); HtmlDocument document = web.Load(currentURL); HtmlNode page = document.DocumentNode; //IEnumerable<HtmlNode> titles = page.Descendants().Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("lvtitle")); IEnumerable<HtmlNode> searchResults = page.Descendants().Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Contains("sresult lvresult")); // Each node represents one search result foreach (HtmlNode searchResult in searchResults) { // Extract title and price, add them to the DataGrid titleNode = searchResult.SelectSingleNode("./h3[@class='lvtitle']"); priceNode = searchResult.SelectSingleNode("./ul[@class='lvprices left space-zero']/li[@class='lvprice prc']"); urlNode = titleNode.SelectSingleNode("./a"); if (titleNode != null) { title = titleNode.InnerText.Trim(); if (title.StartsWith("Neues Angebot")) { title = title.Substring(13).Trim(); } } if (priceNode != null) { price = priceNode.InnerText.Trim(); if (price.StartsWith("EUR")) { price = price.Substring(3).Trim(); } } if (url != null) { url = urlNode.Attributes["href"].Value.Trim(); } shopItem = new ShopItem { Title = title, Price = price, Date = date, URL = url, Platform = platform }; resultList.Add(shopItem); } // Extract Next Page Elements nextPageElements = page.Descendants().Where(d => d.Attributes.Contains("class") && d.Attributes["class"].Value.Equals("gspr next")); currentPage++; } // Is there a Next Page button? while (nextPageElements.Count() > 0); this._numberOfLastResults = --currentPage; return resultList; }