Code代码片断(5do8)

GDI+控件线程IO流ADO.NET接口类,函数语法

新主题
c#分析 meta标签
lucene.net高亮,查询
7zip压缩cookie
lucene.net一个建立,查询...
c#获取IP
lucene.net 教程
c#汉字转拼音
.net json 范例
c#格式化日期
获取IIS的所有网站信息
说话自由

首页 » .NET/C# » 类,函数 »

c#分析 meta标签

标签: Match regex Meta spider
string metaKey = String.Empty, metaValue = String.Empty; 
foreach (Match metamatch in Regex.Matches (htmlData, 
  @"<meta\s*(?:(?:\b(\w|-)+\b\s*(?:=\s*(?:""[^""]*""|'" + 
  @"[^']*'|[^""'<> ]+)\s*)?)*)/?\s*>", 
  RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture)) { 
    metaKey = String.Empty; 
    metaValue = String.Empty; 
    // Loop through the attribute/value pairs inside the tag 

    foreach (Match submetamatch in Regex.Matches(metamatch.Value.ToString(), 
      @"(?<name>\b(\w|-)+\b)\s*=\s*(""(?<value>" + 
      @"[^""]*)""|'(?<value>[^']*)'|(?<value>[^""'<> ]+)\s*)+", 
      RegexOptions.IgnoreCase|RegexOptions.ExplicitCapture)) { 

        if ("http-equiv" == submetamatch.Groups[1].ToString().ToLower() ) { 
            metaKey = submetamatch.Groups[2].ToString(); 
        } 
        if ( ("name" == submetamatch.Groups[1].ToString().ToLower() ) 
            && (metaKey == String.Empty) ) {
            // if already set, HTTP-EQUIV overrides NAME

            metaKey = submetamatch.Groups[2].ToString(); 
        } 
        if ("content" == submetamatch.Groups[1].ToString().ToLower() ) { 
            metaValue = submetamatch.Groups[2].ToString(); 
        } 
    } 
    switch (metaKey.ToLower()) { 
        case "description": 
            htmldoc.Description = metaValue; 
            break; 
        case "keywords": 
        case "keyword": 
            htmldoc.Keywords = metaValue; 
            break; 
        case "robots": 
        case "robot": 
            htmldoc.SetRobotDirective (metaValue); 
            break; 
    } 
}

ccdot写于2008-10-30 9:10:35

如果愿意,请留下你观点或者感受...
称呼*
内容*
验证码*