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

No comments:

Post a Comment