Skip to main content
 首页 » 编程设计

c#之如何从 InternetExplorer 对象查看源 HTML

2024年02月20日9lonelyxmas

当我实例化一个 IE 对象并导航到一个 url 时,我不知道如何从该地址获取源 HTML 代码。

这是我正在使用的代码:

SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer(); 
IE.Visible = false; 
IE.Navigate("www.testsite.com"); 

我想要这样的东西:

string source = IE.ToSource(); 

所以我可以检查它的内容。 我能做到吗?谢谢。

请您参考如下方法:

试试看:

SHDocVw.InternetExplorer IE = new SHDocVw.InternetExplorer(); 
IE.Visible = false; 
IE.Navigate("www.testsite.com"); 
mshtml.IHTMLDocument2 htmlDoc  
         = IE.Document as mshtml.IHTMLDocument2; 
string content = htmlDoc.body.outerHTML; 

您可以从 body.parent 属性访问整个 HTML 字符串:

string content = htmlDoc.body.parent.outerHTML; 

You can see a nice example here (the example in c++)