我有一个程序可以对法语网页进行一些屏幕抓取,并找到一个特定的字符串。一旦找到,我拿起那个字符串并保存。返回的字符串显示为User does not have a desktop configured.
或法语显示为L'utilisateur ne dispose pas d'un bureau configuré.
,但实际显示为:L**\x26#39**;utilisateur ne dispose pas d**\x26#39**;un bureau configur**�**.
如何才能将\x26#39
视为撇号{{} 1}}角色。
C#中是否有可用于阅读Url并返回正确短语的内容。
我已经查看了许多可用的C#功能,但找不到能够为我提供正确结果的功能。
示例代码尝试使用:
'
这会返回// translated the true French text to English to help out with this example.
//
Encoding winVar1252 = Encoding.GetEncoding(1252);
Encoding utf8 = Encoding.UTF8;
Encoding ascii = Encoding.ASCII;
Encoding unicode = Encoding.Unicode;
string url = String.Format("http://www.My-TEST-SITE.com/);
WebClient webClient = new WebClient();
webClient.Encoding = System.Text.Encoding.UTF8;
string result = webClient.DownloadString(url);
cVar = result.Substring(result.IndexOf("Search_TEXT=")).Length ;
result = result.Substring(result.IndexOf("Search_TEXT="), cVar);
result = WebUtility.HtmlDecode(result);
result = WebUtility.UrlDecode(result);
result = result.Substring(0, result.IndexOf("Found: "));
什么时候应该返回:L**\x26#39**;utilisateur ne dispose pas d**\x26#39**;un bureau configur**�**.
。
我正在尝试摆脱L'utilisateur ne dispose pas d'un bureau configuré.
并获取正确的法语字符以显示为\x26#39
等。
答案 0 :(得分:0)
我不能确定,但是:
result = result.Substring(result.IndexOf("Search_TEXT="), cVar);
result = WebUtility.HtmlDecode(result);
result = WebUtility.UrlDecode(result);
双重解码文字不能很好。它既可以是URL,也可以是HTML或两者都不是。不是两个。
答案 1 :(得分:0)
您的第一个问题似乎不是使用字符编码,而是使用某人"\x" escaped sequence和隐藏html entities的自定义组合。
那个有趣的**\x26#39**;
实际上只是一个简单的单引号。翻译后的十六进制字符\x26
变为&
,因此您获得**'**;
。删除无关的星星,你得到html实体'
。使用 HtmlDecode ,这就变成了简单的撇号'
,它只是ascii character 39.。
试试这个代码段。请注意,只有最后一步才能执行HtmlDecode。
var input = @"L**\x26#39**;utilisateur ne dispose pas d**\x26#39**;un bureau configur**�**";
var result = Regex.Replace(input, @"\*\*([^*]*)\*\*", "$1"); // Take out the extra stars
// Unescape \x values
result = Regex.Replace(result,
@"\\x([a-fA-F0-9]{2})",
match => char.ConvertFromUtf32(Int32.Parse(match.Groups[1].Value,
System.Globalization.NumberStyles.HexNumber)));
// Decode html entities
result = System.Net.WebUtility.HtmlDecode(result);
输出为L'utilisateur ne dispose pas d'un bureau configur�
第二个问题是带有重音的“e”。这实际上是一个编码问题,你可能不得不继续玩它以使其正确。您可能还想尝试 UTF16 甚至 UTF32 。但是HtmlAgilityPack可能会自动为您解决这个问题。