欢迎来到山村网

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

2019-03-28 23:33:58浏览:191 来源:山村网   
核心摘要:  Create a console application in any version of Visual Studio using .Netversion 1|2|3|3.5.  Add two Com object refe

  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文档 山村

  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:

  程序截图:

  代码:

  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);

  }

  }

  }

  }

  }

(责任编辑:豆豆)
下一篇:

HTML.Citifaud.j

上一篇:

PHP分页、防止英文单词被截段、去除HTML代码

  • 信息二维码

    手机看新闻

  • 分享到
打赏
免责声明
• 
本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们 xfptx@outlook.com