如何从网页中提取动态ajax内容 [英] How to extract dynamic ajax content from a web page

查看:32
本文介绍了如何从网页中提取动态ajax内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的要求是从网页中提取所需的内容.该页面有一个部分正在使用 ajax 进行填充.当我在页面源中查看时,它没有显示使用 ajax 加载的内容.部分内容将根据选中的复选框而变化.如果我们选中印度"复选框,则该部分将显示印度的所有详细信息.页面源将只显示默认内容,而不是使用 ajax 显示的内容.选中复选框后我检查了页面源,它​​仍然只显示默认值.如何获取该部分内容,

My requirement is to extract the required content from a web page. The page has a section which is being populated using ajax. When i view in page source it is not showing the content loaded using ajax. The section content will change based on check box selected. If we select 'India' check box then the section will display all the details of India. The page source will show only default content not the content displayed using ajax. I checked the page source after selecting the check box, still it shows only default value. How to get that section content,

推荐答案

在 C# 中可以使用 HTMLAgilityPack 抓取数据,但是如果使用 webBrowser.DocumentText,则无法从网页加载 ajax 内容获取 xpath.所以在 webBrowser 控件完全加载网页之后.在 Document_Complete 方法中,您添加以下代码:

In C# you can use HTMLAgilityPack to craw data, but if you use webBrowser.DocumentText, you can't load ajax content from webpage to get xpath. So after webBrowser control loaded webpage completely. In Document_Complete method you add some codes below:

HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
this.webBrowser1.Document;
IHTMLDocument2 currentDoc =(IHTMLDocument2)this.webBrowser1.Document.DomDocument;

doc.LoadHtml(currentDoc.activeElement.innerHTML);

这篇关于如何从网页中提取动态ajax内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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