【发布时间】: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