Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
//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
You can’t perform that action at this time.