asp.net 获取HTML meta标记中的内容
public static string GetSingleTagValueByAttr(string inputstring, string tagName, string attrname, string key)
{
Regex reg = new Regex(“<” + tagName + ” [^<>]*>”, RegexOptions.IgnoreCase);
MatchCollection matchs = reg.Matches(inputstring);
string result = string.Empty;
foreach (Match match in matchs)
{
string matchValue = match.Value;
Regex regValue = new Regex(“content=”.*”", RegexOptions.IgnoreCase);
if (matchValue.ToLower().IndexOf(attrname.ToLower() + “=”" + key.ToLower() + “”") != -1)
{
if (regValue.IsMatch(matchValue))
{
result = regValue.Match(matchValue).Value;
if (!string.IsNullOrEmpty(result))
{
result = result.Replace(“CONTENT=”, “”).Replace(“content=”,”").Replace(“”", “”);
}
}
return result;
}
}
return null;
}
\n
使用举例:
\n
获取关键词 GetSingleTagValueByAttr(data, “meta”, “name”, “Keywords”); data是HTML源代码
获取描述GetSingleTagValueByAttr(data, “meta”, “name”, “Discription”);
\n
来源:http://blog.csdn.net/slimboy123
\n