如何在C#中獲得HTML編碼?

c# encoding html html-agility-pack webclient

我正試圖從網絡詞典中獲取某些單詞的發音。例如,在下面的代碼中,我想從http://collinsdictionary.com獲得good發音

(此處使用HTTP Agility Pack

static void test()
{
    String url = "http://www.collinsdictionary.com/dictionary/english/good";
    WebClient client = new WebClient();
    client.Encoding = System.Text.Encoding.UTF8;
    String html = client.DownloadString(url);
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    doc.LoadHtml(html);
    HtmlAgilityPack.HtmlNode node = doc.DocumentNode.SelectSingleNode("//*[@id=\"good_1\"]/div[1]/h2/span/text()[1]");
    if (node == null)
    {
        Console.WriteLine("XPath not found.");
    }
    else
    {
        Console.WriteLine(node.WriteTo());
    }
}

我在期待

 (É¡ÊŠd

但我最多能得到的是

 (É¡?d

如何做到對了?

一般承認的答案

問題不在於解析文本,而是控制台輸出存在問題。如果從命令行應用程序執行此操作,則可以將控制台的輸出編碼設置為unicode:

Console.OutputEncoding = System.Text.Encoding.Unicode;

您還需要確保控制台中的字體是具有unicode支持的字體。有關詳細信息,請參閱此答案


熱門答案

如果您知道頁面編碼(例如System.Text.Encoding.UTF8);

string html = DownloadSmallFiles_String(url, System.Text.Encoding.UTF8, 20000);

或使用自動編碼檢測(取決於服務器響應)

string html = DownloadSmallFiles_String(url, null, 20000);

最後加載html

doc.LoadHtml(html);

試試下面的代碼

    static void test()
    {
        String url = "http://www.collinsdictionary.com/dictionary/english/good";


        System.Text.Encoding PageEncoding = null; //System.Text.Encoding.UTF8 
        //PageEncoding = null; it means try to detect encoding automatically

        string html = DownloadSmallFiles_String(url, PageEncoding, 20000);


        HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();


        //doc.LoadHtml(html);
        doc.LoadHtml(html);

        HtmlAgilityPack.HtmlNode node = doc.DocumentNode.SelectSingleNode("//*[@id=\"good_1\"]/div[1]/h2/span/text()[1]");
        if (node == null)
        {
            Console .WriteLine("XPath not found.");
        }
        else
        {
            Console.WriteLine(node.WriteTo());
        }
    }

    private static HttpWebRequest CreateWebRequest(string url, int TimeOut = 20000)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko";
        request.Method = "GET";
        request.Timeout = TimeOut;
        request.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
        request.KeepAlive = false;
        request.UseDefaultCredentials = true;
        request.Proxy = null;//ProxyHelperClass.GetIEProxy;
        return request;
    }

    public static string DownloadSmallFiles_String(string Url, System.Text.Encoding ForceTextEncoding_SetThistoNothingToUseAutomatic, int TimeOut = 20000)
    {
        try
        {
            string ResponsString = "";
            HttpWebRequest request = CreateWebRequest(Url, TimeOut);

            using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
            {
                if (response.StatusCode == HttpStatusCode.OK)
                {
                    using (Stream receiveStream = response.GetResponseStream())
                    {
                        if (ForceTextEncoding_SetThistoNothingToUseAutomatic != null)
                        {
                            ResponsString = new StreamReader(receiveStream, ForceTextEncoding_SetThistoNothingToUseAutomatic).ReadToEnd();
                        }
                        else
                        {
                            if (string.IsNullOrEmpty(response.CharacterSet) == false)
                            {
                                System.Text.Encoding respEncoding = System.Text.Encoding.GetEncoding(response.CharacterSet);
                                ResponsString = new StreamReader(receiveStream, respEncoding).ReadToEnd();
                            }
                            else
                            {
                                ResponsString = new StreamReader(receiveStream).ReadToEnd();
                            }
                        }
                    }
                }
            }

            return ResponsString;
        }
        catch (Exception ex)
        {
            return "";
        }
    }


許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因
許可下: CC-BY-SA with attribution
不隸屬於 Stack Overflow
這個KB合法嗎? 是的,了解原因