手机
当前位置:查字典教程网 >网页设计 >脚本HTML教程 >用C#从IE浏览器中获取HTML文档
用C#从IE浏览器中获取HTML文档
摘要:CreateaconsoleapplicationinanyversionofVisualStudiousing.Netversion1|2...

 Create a console application in any version of Visual Studio using .Net version 1|2|3|3.5.

Add two Com object references which will allow us to manipulate IE.

用 Visual Studio 的任意版本建立一个控制台程序。

添加2个COM对象引用用来操作IE

用C#从IE浏览器中获取HTML文档1

Note the code sample below does not require the using directive for the objects, so just add the code as is.

Then find the instances of IE and extract the document:

添加下面代码

打开IE获取HTML文档

SHDocVw.ShellWindows shellWindows

= new SHDocVw.ShellWindowsClass();

string filename;

foreach (SHDocVw.InternetExplorer ie in shellWindows)

{

filename

= Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

if (filename.Equals("iexplore"))

{

Console.WriteLine("Web Site : {0}", ie.LocationURL);

mshtml.IHTMLDocument2 htmlDoc

= ie.Document as mshtml.IHTMLDocument2;

Console.WriteLine(" Document Snippet: {0}",

( ( htmlDoc != null ) ? htmlDoc.body.outerHTML.Substring(0, 40)

: "***Failed***" ));

Console.WriteLine("{0}{0}", Environment.NewLine);

}

}

Here is a screen-shot of the output:

程序截图:

用C#从IE浏览器中获取HTML文档2

代码:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace ConsoleApplication1

{

class Program

{

static void Main(string[] args)

{

SHDocVw.ShellWindows shellWindows = new SHDocVw.ShellWindowsClass();

string filename;

foreach (SHDocVw.InternetExplorer ie in shellWindows)

{

filename = Path.GetFileNameWithoutExtension(ie.FullName).ToLower();

if (filename.Equals("iexplore"))

{

Console.WriteLine("Web Site: {0}", ie.LocationURL);

mshtml.IHTMLDocument2 htmlDoc = ie.Document as mshtml.IHTMLDocument2;

Console.WriteLine("文件 Snippet: {0}", ((htmlDoc != null) ? htmlDoc.body.outerHTML.Substring(0, 40) : "***Failed***"));

Console.WriteLine("{0}{0}", Environment.NewLine);

}

}

}

}

}

【用C#从IE浏览器中获取HTML文档】相关文章:

HTML语言剖析(七) 表格标记

(X)HTML Strict 下的嵌套规则

使用Tiger交叉引用HTML

XHTML+CSS兼容性解决的五个方案

HTML 几种特别分割线特效

html文档中的 ol 元素的序号数字极限探讨

IE浏览器HTML Hack标签总结

如何从Html页面中提取所有汉字

PHP生成静态HTML文章发布系统

XUL中如何插入一段HTML内容

精品推荐
分类导航