如何从InternetExplorer对象查看源代码的HTML?源代码、对象、InternetExplorer、HTML

2023-09-03 04:27:50 作者:溺于眉间星河

当我实例化一个IE浏览器对象并导航到一个网址,我不知道如何获得该地址的HTML源$ C ​​$ C。

When I instantiate an IE object and navigate to a url, I don't know how to obtain the source HTML code from that address.

这是在code我使用:

This is the code I'm using:

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

我想是这样的:

I want something like:

string source = IE.ToSource();

所以,我可以检查它的内容。 我能做到这一点?谢谢你。

So I can inspect the content of it. Can I achieve this? Thanks.

推荐答案

试试:

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字符串:

You can access the whole HTML string from the body.parent property:

string content = htmlDoc.body.parent.outerHTML;

你可以看到一个很好的例子这里(++的例子C)