【问题标题】:Trying to convert string to proper format / encoding?试图将字符串转换为正确的格式/编码?
【发布时间】:2014-01-08 02:08:14
【问题描述】:

我有一个程序可以对法语网页进行一些屏幕抓取并找到特定的字符串。一旦找到我就拿那个字符串并保存它。返回的字符串显示为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 视为撇号' 字符。

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 并让正确的法语字符显示为 é ê è ç â 等。

【问题讨论】:

  • 您不想使用 HtmlAgilityPack 之类的适当工具进行 Web 清理的任何特殊原因?
  • 你把很多东西混在一起了。基本上,UTF8 是字符的编码方式,而 Unicode 是表示。我建议你先阅读这篇关于这个的精彩文章,你就会明白发生了什么。 joelonsoftware.com/articles/Unicode.html
  • 我不知道“HtmlAgilityPack”,现在正在阅读文档。至于 Joel 网站……是的,我已经看过了,但它并没有告诉我为什么我的屏幕废料中仍然没有看到 UTF8 代码。试图找到完美的代码来让我得到正确的文本。
  • @MaximilianoRios - 文章链接加 1。
  • 不客气,我想我们都应该阅读这类关于此事背景的文章。正确理解代码非常重要。

标签: c# string encoding character


【解决方案1】:

我不能确定,但​​是:

result = result.Substring(result.IndexOf("Search_TEXT="),  cVar);
result = WebUtility.HtmlDecode(result);
result = WebUtility.UrlDecode(result);

对文本进行双重解码并不好。它要么是 URL,要么是 HTML,要么都不是。两者都不是。

【讨论】:

  • 试过了:result = WebUtility.HtmlDecode(result); // 结果 = WebUtility.UrlDecode(result);然后 // 结果 = WebUtility.HtmlDecode(result);结果 = WebUtility.UrlDecode(结果); UrlDecode 单独给了我一个关于字符串大小的错误。
【解决方案2】:

看起来您的第一个问题不是字符编码,而是某人自定义的"\x" escaped sequence 和模糊的html entities 组合。

那个有趣的**\x26#39**;实际上只是一个简单的单引号。翻译后的十六进制字符\x26 变为&,所以你得到**&#39**;。去掉多余的星星,你会得到 html 实体'。使用 HtmlDecode,这变成了简单的撇号 ',它只是 ascii 字符 39。

试试这个 sn-p。请注意,只有最后一步我们才能进行 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 可能会自动为您处理这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-01
    • 1970-01-01
    • 2012-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 2017-10-22
    相关资源
    最近更新 更多