- r - 以节省内存的方式增长 data.frame
- ruby-on-rails - ruby/ruby on rails 内存泄漏检测
- android - 无法解析导入android.support.v7.app
- UNIX 域套接字与共享内存(映射文件)
我在为我正在构建的屏幕抓取工具从网上下载一些内容时遇到问题。
在下面的代码中,从 Web 客户端下载字符串方法返回的字符串返回一些奇怪的字符用于少数(不是所有)网站的源下载。
我最近添加了 http header ,如下所示。以前在没有 header 的情况下调用相同的代码达到相同的效果。我没有尝试过“Accept-Charset” header 的变体,除了基础知识之外,我对文本编码了解不多。
我指的字符或字符序列是:
“”
和
“”
当您在网络浏览器中使用“查看源代码”时,看不到这些字符。可能是什么原因造成的,我该如何解决这个问题?
string urlData = String.Empty;
WebClient wc = new WebClient();
// Add headers to impersonate a web browser. Some web sites
// will not respond correctly without these headers
wc.Headers.Add("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-GB; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12");
wc.Headers.Add("Accept", "*/*");
wc.Headers.Add("Accept-Language", "en-gb,en;q=0.5");
wc.Headers.Add("Accept-Charset", "ISO-8859-1,utf-8;q=0.7,*;q=0.7");
urlData = wc.DownloadString(uri);
最佳答案

是八位字节 EF BB BF
的 windows-1252 表示。那是 the UTF-8 byte-order marker ,这意味着您的远程网页是用 UTF-8 编码的,但您正在阅读它,就好像它是 windows-1252 一样。 According to the docs , WebClient.DownloadString
使用 Webclient.Encoding
在将远程资源转换为字符串时作为其编码。将其设置为 System.Text.Encoding.UTF8
理论上应该可以正常工作。
关于c# - WebClient.DownloadString() 返回带有特殊字符的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4716470/
我想从谷歌财经导入文本数据,我用这个http地址作为参数来DownloadString http://www.google.com/finance/getprices?i= 1200&p=1d&f=d
我有一个简单的功能可以从特定页面的以下代码中抓取突然出现 503 错误的网页: try { WebClient client = new WebClient(); client.Proxy =
我正在尝试从 Amazon 下载 html 文档,但由于某种原因,我得到了一个错误的编码字符串,例如“�� K��g��g�e”。 这是我尝试过的代码: using (var webClient =
我有一个名为 GetIP 的函数,我在启动时和用户按下按钮时调用该函数。由于某种原因,它在启动时不会崩溃,但在使用按钮调用该函数时会崩溃。没有异常(exception),什么都不会卡住。 函数代码:
这个问题在这里已经有了答案: C# WebClient disable cache (12 个答案) 关闭 7 年前。 我正在使用这段代码从 URL 获取返回字符串 webClient.Encodi
只是一段代码 WebClient wc = new WebClient(); String str = wc.DownloadString(new Uri("http://content.warfra
我正在开发一个使用 ASP.Net 3.5 运行的 Web 应用程序 在应用程序的某处,我正在调用外部系统。此调用包括从特定 url 下载字符串: string targetUrl = BuildMy
我遇到了一个关于 WebClient.DownloadString 的奇怪问题,我似乎无法解决,我的代码: Dim client As New WebClient() Dim html = clien
这个问题在这里已经有了答案: WebClient.DownloadString result is not match with Browser result 2 (3 个答案) 关闭 5 年前。
WebClient client = new WebClient(); string url = "https://someurl.com/..." string get = client.Dow
我正在使用 webclient 从在线共享点下载 XML 文件。 但是,当我使用 WebClient.DownloadString(string url) 方法时,一些字符没有被正确解码。 当我使用
我正在尝试查看 http://simpledesktops.com/browse/desktops/2012/may/17/where-the-wild-things-are/ 的来源使用代码: St
对 WebClient.DownloadString 的调用给出了第一次机会异常: IOException: The specified registry key does not exist. 一段
我想从海盗湾的搜索查询中获取源代码,我的代码中有这个但它没有返回任何内容: WebClient webpage = new WebClient(); string source= webpage.D
我在为我正在构建的屏幕抓取工具从网上下载一些内容时遇到问题。 在下面的代码中,从 Web 客户端下载字符串方法返回的字符串返回一些奇怪的字符用于少数(不是所有)网站的源下载。 我最近添加了 http
任何人都知道我可以如何修改下面的 DownloadString 以便我也可以传递基本的身份验证凭据? 我正在使用与 PsGet (http://psget.net/) 相同的技术来下载我自己的内部脚本
我正在使用 WebClient.DownloadString() 方法下载一些数据。我正在使用以下代码: static void Main(string[] args) { s
我昨天开始使用 Xamarin for Android,是的,它非常好。当然,我和每个人一样,肯定会有一些错误。 我尝试的只是使用以下代码行异步下载 www.google.com 的 HTML:
我想知道在使用 WebClient.DownloadString 时我应该保护自己免受哪些异常的影响。 这是我目前使用它的方式,但我相信你们可以建议更好、更健壮的异常处理。 例如,在我的脑海中: 没有
我正在尝试使用正则表达式从网站检索名称。但是,当我运行该程序时,使用“路径中的非法字符”出现错误。这是代码: private void button1_Click(object sender, Eve
我是一名优秀的程序员,十分优秀!