C#和HtmlAgilityPack编码的问题 [英] C# and HtmlAgilityPack encoding problem

查看:257
本文介绍了C#和HtmlAgilityPack编码的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

WebClient GodLikeClient = new WebClient();
HtmlAgilityPack.HtmlDocument GodLikeHTML = new HtmlAgilityPack.HtmlDocument();

GodLikeHTML.Load(GodLikeClient.OpenRead("www.alfa.lt");

所以这个代码返回:Skaitytojo klausimas psichologui:KAS lemiahomoseksualumÄ... - Naujienųportalas Alfa.lt?而不是Skaitytojo klausimas psichologui:KAS lemiahomoseksualumą - Naujienųportalas Alfa.lt。

So this code returns: "Skaitytojo klausimas psichologui: kas lemia homoseksualumÄ…? - Naujienų portalas Alfa.lt" instead of "Skaitytojo klausimas psichologui: kas lemia homoseksualumą? - Naujienų portalas Alfa.lt".

本网页被编码为1257(波罗的海),但 textBox1.Text = GodLikeHTML.DocumentNode.OuterHtml; 返回扭曲文本 - 波罗的海变音符号转化为一些奇怪的几个字符长字符串:(

This webpage is encoded in 1257 (baltic), but textBox1.Text = GodLikeHTML.DocumentNode.OuterHtml; returns the distorted text - baltic diacritics are transformed into some weird several characters long strings :(

是的,我已经试过了HtmlAgilityPack论坛。他们吸。

And yes, I've tried the HtmlAgilityPack forums. They do suck.

PS我不是程序员,但我在一个社区项目工作,我真的需要得到这个代码的工作感谢;}

P.S. I'm no programmer, but I work on a community project and I really need to get this code working. Thanks ;}

推荐答案

其实页面编码使用UTF-8。

Actually the page is encoded with UTF-8.

GodLikeHTML.Load(GodLikeClient.OpenRead("http://www.alfa.lt"), Encoding.UTF8);

将工作

或者你可以使用代码在我的 SO回答检测从HTTP头或meta标签的编码,连接正确重新编码。 (它也支持gzip来减少你下载)

Or you could use the code in my SO answer which detects encoding from http headers or meta tags, en re-encodes properly. (It also supports gzip to minimize your download).

通过下载类的代码看起来像:

With the download class your code would look like:

HttpDownloader downloader = new HttpDownloader("http://www.alfa.lt",null,null);
GodLikeHTML.LoadHtml(downloader.GetPage());

这篇关于C#和HtmlAgilityPack编码的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆