Forked from scorta/Get Title and Meta Description tag from an url c#
Created
March 15, 2018 18:36
-
-
Save paladique/d1c57ced71372e3ccc9b8fcf28db02b3 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//First you need to retrieve html source of the given url | |
//Get Url Title | |
private string UrlTitle(string url) | |
{ | |
string source = HtmlSrc(url); | |
string title = Regex.Match(source, @"\<title\b[^>]*\>\s*(?<Title>[\s\S]*?)\</title\>", RegexOptions.IgnoreCase).Groups["Title"].Value; | |
return title; | |
} | |
//Get Url Meta Description tag | |
private string GetMetaTagValuesUtf8(string url) // use HTML Agility, run good with all page which have Meta Desc tag | |
{ | |
//Get Meta Tags | |
try | |
{ | |
string src = string.Empty; | |
try | |
{ | |
src = HtmlSrc(url); | |
} | |
catch | |
{ | |
return "cannot connect"; | |
} | |
string tempSrc = src.Replace("<div", "|<div"); | |
tempSrc = Replace(tempSrc, " "); | |
tempSrc = tempSrc.Trim(); | |
var doc = new HtmlAgilityPack.HtmlDocument(); | |
doc.LoadHtml(tempSrc.Replace("<", " <")); | |
try | |
{ | |
var metaTags = doc.DocumentNode.SelectNodes("//meta"); | |
if (metaTags != null) | |
{ | |
foreach (var tag in metaTags) | |
{ | |
if (((tag.Attributes["name"] != null) || (tag.Attributes["Name"] != null) || (tag.Attributes["NAME"] != null)) && ((tag.Attributes["content"] != null) || (tag.Attributes["Content"] != null) || (tag.Attributes["CONTENT"] != null)) && ((tag.Attributes["name"].Value == "description") || (tag.Attributes["name"].Value == "Description") || (tag.Attributes["name"].Value == "DESCRIPTION") || (tag.Attributes["Name"].Value == "description") || (tag.Attributes["Name"].Value == "Description") || (tag.Attributes["Name"].Value == "DESCRIPTION") || (tag.Attributes["NAME"].Value == "description") || (tag.Attributes["NAME"].Value == "Description") || (tag.Attributes["NAME"].Value == "DESCRIPTION"))) | |
{ | |
string temp = tag.Attributes["content"].Value; | |
if (temp == null) temp = tag.Attributes["Content"].Value; | |
if (temp == null) temp = tag.Attributes["CONTENT"].Value; | |
return temp; | |
} | |
} | |
} | |
} | |
catch | |
{ | |
return "null"; | |
} | |
} | |
catch | |
{ | |
return "null"; | |
} | |
return "null"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment