Monday 23 January 2017

Search Keyword Functionality C# code - SEO Beginners

#Author : mounicraju@gmail.com
# Paste the below code in Search Functionality method
#Below code searches the keyword that user enters to search in complete website placed in excel and written's output in proper manner in a excel with following attributes like Founded on website, what type it is and if it is link.
# Data table -> Processes -> Excel Output

--- Code starts from here

                    DataTable dataTable = new DataTable();
                    dataTable.Columns.Add("Type");
                    dataTable.Columns.Add("Found On");
                    dataTable.Columns.Add("Sentence Preview");
                    dataTable.Columns.Add("Link (if available)");

                    string option = SearchKeywordOption.SelectedValue;
                    string value = SearchKeywordValue.Text;
                    foreach (DataRow row in DtTbl.Rows)
                    {
                        string val = row["Extractor 1 1"].ToString();
                        HtmlDocument htmlDoc = new HtmlDocument();
                        htmlDoc.LoadHtml(val);
                        List<HtmlNode> nodes = new List<HtmlNode>();
                        if (option == "All")
                        {
                            if (htmlDoc.DocumentNode.SelectNodes("//h1 | //h2") != null)
                                nodes = htmlDoc.DocumentNode.SelectNodes("//h1 | //h2").Where(x => x.InnerText.Contains(value)).ToList();
                        }
                        else if (htmlDoc.DocumentNode.SelectNodes("//" + option.ToLower()) != null)
                            nodes = htmlDoc.DocumentNode.SelectNodes("//" + option.ToLower()).Where(x => x.InnerText.Contains(value)).ToList();
                        foreach (HtmlNode node in nodes)
                        {
                            string RegexPattern = @"<a.*?href=[""'](?<url>.*?)[""'].*?>(?<name>.*?)</a>";
                            MatchCollection matches = Regex.Matches(node.InnerHtml, RegexPattern, RegexOptions.IgnoreCase);
                            if (matches.Count == 0)
                                dataTable.Rows.Add(new object[] { option, row["Address"], GetMatchedString(node.InnerText, val), "" });
                            else
                                dataTable.Rows.Add(new object[] { option, row["Address"], GetMatchedString(matches[0].Groups["url"].Value, val), matches[0].Groups["url"].Value });
                        }
                    }
                    SearchKeyWordResult.DataSource = dataTable;
                    SearchKeyWordResult.DataBind();
                    dtSet.Tables.Add(dataTable);

1 comment:

  1. An SEO's starting point depends on what you input into the search engine. Choose poorly, and you are doomed to failure completely missing the people who are looking for your website/product/brand.
    Source: suggestions box scraper

    ReplyDelete