【问题标题】:Getting a dynamic web page source in C #在 C# 中获取动态网页源
【发布时间】:2013-11-05 14:41:56
【问题描述】:

如何使用 C# 下载动态网页源?更具体地说,例如,我有一个页面http://example.com。下载了源码,但是因为AJAX,给源码增加了几行,收藏后没有得到我想要的。有谁知道如何“刷新”源代码,或者是否有办法实现这样的目标?您现有的“静态”代码:

WebClient client = new WebClient();
Byte[] pageData = client.DownloadData("http://example.com" + address);
string pageHtml = Encoding.UTF8.GetString(pageData);
Console.WriteLine(pageHtml);
Console.ReadKey();

问候。

【问题讨论】:

  • 为什么我有预感这是出于邪恶的数据挖掘目的? :P
  • 您需要使用执行 JavaScript 的客户端。一个简单的WebClient 对象不会这样做。 WebBrowser 对象可能。
  • @Moo Juice - 不。我只是 C# 的初学者,我想创建一个简单的应用程序,例如,当您输入德语单词时,给我对应的英语单词。这些只是我的网络使用练习。虽然我不确定我是否可以做这样的事情,因为从我读到的内容来看,通常的谷歌翻译 API 是付费的
  • 必应翻译 API 在一定程度上是免费的:microsoft.com/web/post/using-the-free-bing-translation-apis

标签: c# web


【解决方案1】:

您可以使用 WebBrowser 组件创建表单。假设您将其命名为browser

private void PrepareDocument()
{
   browser.Navigate("http://somewebsite.com");
   var timer = new Timer(1000);
   timer.Elapsed += new ElapsedEventHandler(timer_Elapsed);
   timer.Enabled = true;
}

private void timer_Elapsed(object sender, ElapsedEventArgs e)
{
   //parse the document, find the data that should be loaded after ajax call
   if(browser.ReadyState == WebBrowserReadyState.Complete && 
      browser.Document.GetElementById("ajax-divId") != null)
   {
      timer.Enabled=false;
      ProceedOnDocument();
   }
}

private void ProceedOnDocument()
{
   //your code here
}

【讨论】:

    猜你喜欢
    • 2018-07-24
    • 1970-01-01
    • 2010-12-21
    • 2021-04-18
    • 2015-03-24
    • 1970-01-01
    • 1970-01-01
    • 2012-09-27
    • 1970-01-01
    相关资源
    最近更新 更多