简体   繁体   English

如何获取 UTF-8 格式的网站标题?

[英]How to get title of a website in UTF-8 format?

I want to get title of a website.我想获得一个网站的标题。 I'm using client.Encoding , It's almost perfect but there's something wrong.我正在使用client.Encoding ,它几乎是完美的,但有一些问题。

It returns me "Budapeşte&#039de gezilecek yerler | Skyscanner Haberler" but the title has apostrophe instead of unicode.它返回给我“Budapeşte&#039de gezilecek yerler | Skyscanner Haberler”但标题有撇号而不是unicode。

The Turkish character "ş" is OK.土耳其语字符“ş”是可以的。

string baslikCek()
        {
            Uri url = new Uri("https://www.skyscanner.com.tr/haberler/budapestede-gezilecek-yerler");
            WebClient client = new WebClient();
            client.Encoding = Encoding.UTF8;
            string html = client.DownloadString(url);
            HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
            doc.LoadHtml(html);
            String title = (from x in doc.DocumentNode.Descendants()
                            where x.Name.ToLower() == "title"
                            select x.InnerText).FirstOrDefault().ToString();
            return title;
        }

Your example shown here is incorrect, &#039 is missing the trailing ;此处显示的示例不正确, &#039缺少尾随; . .

But it is correct from the server, so you may do this:但它从服务器是正确的,所以你可以这样做:

return System.Net.WebUtility.HtmlDecode(title);

This is not the same as Encoding.UTF8 , which is the binary encoding of the string data.这与Encoding.UTF8 ,后者是字符串数据的二进制编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM