简体   繁体   中英

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.

It returns me "Budapeşte&#039de gezilecek yerler | Skyscanner Haberler" but the title has apostrophe instead of 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 ; .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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