Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, 23 January 2017

C# code to find external links - Technical SEO Beginners

#Author : mounicraju@gmail.com
#Use the below code to find the external links on any website.
#Replace the word "mounic" with your defined domain

 HtmlDocument htmlDoc = new HtmlDocument();
                        htmlDoc.LoadHtml(val);
                        List<HtmlNode> nodes = new List<HtmlNode>();
                        if (htmlDoc.DocumentNode.SelectNodes("//a") != null)
                            nodes = htmlDoc.DocumentNode.SelectNodes("//a").ToList();
                        foreach (HtmlNode node in nodes)
                        {
                            if (node.Attributes["href"] != null)
                            {
                                if (node.Attributes["href"].Value.Contains("http") && !node.Attributes["href"].Value.Contains("mounic"))
                                {
                                    if (option == "All")
                                    {
                                        if (node.Attributes["rel"] != null)
                                            dataTable.Rows.Add(new object[] { node.Attributes["rel"].Value, row["Address"], node.Attributes["href"].Value, node.InnerText, GetMatchedString(node.Attributes["href"].Value, val) });
                                        else
                                            dataTable.Rows.Add(new object[] { "Follow", row["Address"], node.Attributes["href"].Value, node.InnerText, GetMatchedString(node.Attributes["href"].Value, val) });
                                    }
                                    else if (option == "Follow")
                                    {
                                        if (node.Attributes["rel"] == null)
                                            dataTable.Rows.Add(new object[] { option, row["Address"], node.Attributes["href"].Value, node.InnerText, GetMatchedString(node.Attributes["href"].Value, val) });
                                        else if (node.Attributes["rel"].Value.ToLower() == option.ToLower())
                                            dataTable.Rows.Add(new object[] { option, row["Address"], node.Attributes["href"].Value, node.InnerText, GetMatchedString(node.Attributes["href"].Value, val) });
                                    }
                                    else
                                    {
                                        if (node.Attributes["rel"] != null)
                                            if (node.Attributes["rel"].Value.ToLower() == option.ToLower())
                                                dataTable.Rows.Add(new object[] { option, row["Address"], node.Attributes["href"].Value, node.InnerText, GetMatchedString(node.Attributes["href"].Value, val) });
                                    }
                                }
                            }
                        }
                    }

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);

C# Script to get Wikipedia Info box data for bulk number of companies with one click



using HtmlAgilityPack;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Reflection;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace wikicontent
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        public class Pageval
        {
            public int pageid { get; set; }
            public int ns { get; set; }
            public string title { get; set; }
            //public string extract { get; set; }
            public dynamic revisions { get; set; }
        }
        public class Query
        {
            public Dictionary<string, Pageval> pages { get; set; }
        }
        public class Limits
        {
            public int extracts { get; set; }
        }
        public class RootObject
        {
            public string batchcomplete { get; set; }
            public Query query { get; set; }
            //public Limits limits { get; set; }
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string[] companies = TextArea1.InnerText.Split(',');
            StringBuilder sb = new StringBuilder();
            foreach (string company in companies)
            {
                string url = "https://en.wikipedia.org/wiki/" + company.Trim(); // API Call
                HtmlWeb hw = new HtmlWeb();
                HtmlDocument doc = hw.Load(url);
                var table = doc.DocumentNode.SelectNodes("//table")[0];
                sb.Append("<tr><td>" + company + "</td><td>" + table.OuterHtml + "</td></tr>");
            }
            result.InnerHtml = sb.ToString();
        }
    }
}