/// <summary> /// Form submission /// </summary> /// <param name="uri"></param> /// <param name="keywords"></param> /// <returns></returns> public async Task <JsonResult> Crawl(string uri, string keywords) { // create a new crawler and do a crawl using the provided search terms var crawlerV2 = new CrawlerV2(Address, keywords); await crawlerV2.Search(); var data = crawlerV2.GetResponse(); if (data == string.Empty) { return new JsonResult() { Data = new { error = "Could not retrieve search engine results." } } } ; // parent element const string htmlOfDivContainingLink = "<div class=\"ZINbbc xpd O9g5cc uUPGi\"><div class=\"kCrYT\">"; // child element const string htmlOfAnchorElement = "<div class=\"kCrYT\"><a href=\"/url?q="; // create a new uri locator so we can figure out where all the links are var uriLocator = new ChildElementLocator(data, htmlOfDivContainingLink, htmlOfAnchorElement, uri, new FindIndex()); uriLocator.FindLocationsOfUri(); uriLocator.CountUriOccurrences(); var uriLocations = uriLocator.GetUriLocations(); var uriCount = uriLocator.GetUriCount(); // display "no results" if there are no links to infotrack in the search results if (uriCount == 0) { keywords = $"{keywords} (no results)"; } return(new JsonResult { Data = new { success = new { uri, keywords, uriCount, uriLocations } } }); } }
public async Task CountUriOccurrencesTest_NotNull() { // Arrange const string address = "https://www.google.com.au/search?gl=au&hl=en&pws=0&num=100&q="; const string keywords = "online title search"; var crawlerV2 = new CrawlerV2(address, keywords); await crawlerV2.Search(); var response = crawlerV2.GetResponse(); var parentString = "<div class=\"ZINbbc xpd O9g5cc uUPGi\"><div class=\"kCrYT\">"; var childString = "<div class=\"kCrYT\"><a href=\"/url?q="; var uri = "https://www.infotrack.com.au"; var childElementLocator = new ChildElementLocator(response, parentString, childString, uri); // Act childElementLocator.CountUriOccurrences(); // Assert Assert.IsNotNull(childElementLocator.GetUriCount()); }