我怎么能读通过ajax使用Web客户端生成的内容? [英] How can I read content generated via ajax using webclient?

查看:202
本文介绍了我怎么能读通过ajax使用Web客户端生成的内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Web客户端

public void download()
{
client = new WebClient();
client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
client.Encoding = Encoding.UTF8;
client.DownloadStringAsync(new Uri(eUrl.Text));
}
void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
    SaveFileDialog sd = new SaveFileDialog();
    if (sd.ShowDialog() == DialogResult.OK)
    {
        StreamWriter writer = new StreamWriter(sd.FileName,false,Encoding.Unicode);
        writer.Write(e.Result);
        writer.Close();                
    }
}

这工作正常。但我无法读取正在使用AJAX加载的内容。像这样的:

This works fine. But I am unable to read content that is loaded using ajax. Like this:

<div class="center-box-body" id="boxnews" style="width:768px;height:1167px; ">
    loading ....	</div>

<script language="javascript">
    ajax_function('boxnews',"ajax/category/personal_notes/",'');
    </script>

这从在客户端服务器ajax_function下载数据。

This "ajax_function" downloads data from server on the client side.

我怎样才能下载完整的网页的HTML数据?

How can I download the full web html data?

推荐答案

要做到这一点,你需要举办一个全面的网络浏览器内的JavaScript运行。不幸的是,Web客户端是不能这样做的。

To do so, you would need to host a Javascript runtime inside of a full-blown web browser. Unfortunately, WebClient isn't capable of doing this.

您唯一的选择将是一个WebBrowser控件的自动化。您需要将它发送到URL,等待两个主要页面的的任何AJAX的内容已经被加载(包括触发该载荷是否需要用户采取行动,这样做),然后刮整个DOM

Your only option would be automation of a WebBrowser control. You would need to send it to the URL, wait until both the main page and any AJAX content has been loaded (including triggering that load if user action is required to do so), then scrape the entire DOM.

如果你只刮一个特定的网站,你可能只是拉AJAX URL自己(模拟所需的所有参数),而不是拉网页更好的要求它。

If you are only scraping a particular site, you are probably better off just pulling the AJAX URL yourself (simulating all required parameters), rather than pulling the web page that calls for it.

这篇关于我怎么能读通过ajax使用Web客户端生成的内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆