【问题标题】:webclient doesn't download the web page completelywebclient 没有完全下载网页
【发布时间】:2013-05-31 08:34:37
【问题描述】:

有下载页面的代码:

 System.Net.WebClient client = new System.Net.WebClient();
 client.Headers.Add("user-agent", "Mozilla/20.0.1");
 byte[] feedBytes;
 string url;
 url = @"http://www.marathonbet.co.uk/en/betting/Football";
 string fullPage = string.Empty;
 try
 {
      feedBytes = client.DownloadData(url);
 }
 catch (System.Net.WebException)
 {
      return;
 }
string fullPage = Encoding.UTF8.GetString(feedBytes);

因此,'fullpage' 仅包含页面的一部分。在浏览器中页面的加载是逐渐发生的。如何下载整页?

【问题讨论】:

  • 为什么忽略异常?删除那个 try/catch 块,看看会发生什么。至少,捕获异常并显示ex.ToString(),这样您就知道出了什么问题。
  • 您的代码对我有用。执行您的代码后,我在fullPage 中获得了整页...
  • nemsv - 我想你收到了大约 300 Kb 的信息,整页超过 2 Mb
  • 添加以捕获 'Console.WriteLine("WebException");'。结果没有改变。
  • 问题很可能是页面使用JavaScript生成内容,而WebClient.DownloadData不执行JavaScript;它只是下载那一页。

标签: c# webclient


【解决方案1】:

试试这个

public static string GetHTMLDocument(string url)
        {
            var result = "";
            using (var wc = new WebClient())
            {
                result = wc.DownloadString(url);
            }
            return result;
        }

【讨论】:

  • 仅供参考,您可以使用return wc.DownloadString(url);
  • 没有区别 - 结果只包含页面的一小部分(大约 3-5%)
  • 这意味着,在文档加载之后可能会加载动态内容。您对此无能为力。
  • 不存在下载动态内容的方式?
  • 您不会下载动态内容。您执行生成动态内容的脚本。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-08-15
  • 2019-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多