Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

感谢开源,小反馈 #6

Open
itldg opened this issue Jul 4, 2017 · 0 comments
Open

感谢开源,小反馈 #6

itldg opened this issue Jul 4, 2017 · 0 comments

Comments

@itldg
Copy link

itldg commented Jul 4, 2017

非常感谢开源,测试了几个网页发现一些小问题
注释错误,标题建议匹配H2和H3
另外压缩的网页有换行
H1中的内容有时候并不能被设定为标题
做了小调整

    /// <summary>
    /// 获取网页标题
    /// </summary>
    /// <param name="html">网页代码</param>
    /// <returns>返回处理过的标题</returns>
    private static string GetTitle(string html)
    {
        string titleFilter = @"<title>[\s\S]*?</title>";
        string clearFilter = @"<.*?>";

        string title = "";
        Match match = Regex.Match(html, titleFilter, RegexOptions.IgnoreCase);
        if (match.Success)
        {
            title = Regex.Replace(match.Groups[0].Value, clearFilter, "");
        }

        // 正文的标题一般在h1中,比title中的标题更干净
        for (int i = 1; i < 4; i++)
        {
            string h1Filter = @"<h"+i+ ".*?>[\\s\\S]*?</h" + i+">";
            MatchCollection mcs = Regex.Matches(html, h1Filter, RegexOptions.IgnoreCase);
            if (mcs.Count==1)
            {

                    string h = Regex.Replace(mcs[0].Groups[0].Value, clearFilter, "").Trim();
                    if (!String.IsNullOrEmpty(h) && title.Trim().Contains(h))
                    {
                        title = h;
                    }
                
                
            }
        }
       

        return title;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant