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

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

发布时间:2017-09-12 16:00

来源:互联网

当前栏目:网页设计教程

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对象引用用来操作IEb48a9b6d74ad615b3a9f4db05a559088.png

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:

程序截图:7125bc11965b048522cc6f335ab32b96.png

代码:

using System;

using System.Collections.Generic;

using System.Text;

using System.IO;

namespace ConsoleApplic小调网电影下载[www.aikan.tv/special/xiaodiaowang/]ation1

{

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

}

}

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值