利用mshtml進行網頁分析

How to use mshtml.

目前網路上有很多免費的html parser供人下載使用

教學文章也不少

但是好像還沒有人提到微軟自己本身提供的mshtml

所以就來寫一下吧

 

1. 首先在方案總管中要使用的專案上,按右鍵選擇加入參考

image

 

2. 選擇.NET標籤頁, 然後在下面尋找Microsoft.mshtml, 完畢後按確定

image

 

3. 這樣參考的部分就會多一個 Microsoft.mshtml, 然後記得在程式碼中using這個namespace

image

 

4. 以下就是程式碼範例


	/// <summary>
/// 主要的執行流程
/// </summary>
private void MainProcess()
{
    string szTestURL_ = @"http://tw.dictionary.yahoo.com/dictionary?p=concentrate";

    string szHtmlContent_ = this.DownloadWeb(szTestURL_);

    string szAfterParser_ = this.ParserHtml(szHtmlContent_, "TD");

}


/// <summary>
/// 利用mshtml進行分析
/// </summary>
/// <param name="szHtmlContent"></param>
/// <param name="szFilterTag"></param>
private string ParserHtml(string szHtmlContent, string szFilterTag)
{
    // 首先將網頁內容存入HTMLDocumentClass
    IHTMLDocument2 docHtml_ = new HTMLDocumentClass();
    docHtml_.write(new object[] { szHtmlContent });
    docHtml_.close();


    // 屬性body則是html中的body tag
    // 而body本身就是一個IHTMLElement
    // 所以可以用all這個屬性將所有元素取出成為一個collection
    IHTMLElementCollection col_1_ = (IHTMLElementCollection)docHtml_.body.all;
    
    
    // 可以用tags這個方法過濾出我們所需要的tag
    IHTMLElementCollection col_2_ = (IHTMLElementCollection)col_1_.tags(szFilterTag);


    string szResult_ = "";
    IHTMLElement elem_ = null;

    
    // length這個屬性可以得到目前的集合中的元素數量
    int iCollectionLength = col_2_.length;


    for (int i = 0; i < iCollectionLength; i++)
    {
        // 使用item這個方法可以將集合中的元素取出
        // 第一個參數代表的是順序,但是在msdn中標示為name
        // 第二個參數msdn中標示為index,但經過測試後,指的並不是順序,所以目前無法確定他的用途
        // 如果有知道的朋友,也請跟我說一下
        elem_ = (IHTMLElement)col_2_.item(i, null);
        

        if( elem_.innerHTML ==null)
            continue;

        szResult_ += elem_.innerHTML;
    }

    return szResult_;
}


/// <summary>
/// 下載網頁的方式
/// 網路上很多範例
/// 我在這邊就不綴訴
/// </summary>
/// <param name="szURL"></param>
/// <returns></returns>
private string DownloadWeb(string szURL)
{
    HttpWebRequest reqHttp_ = (HttpWebRequest)WebRequest.Create(szURL);
    reqHttp_.Timeout = 30000;

    HttpWebResponse respHttp_ = (HttpWebResponse)reqHttp_.GetResponse();

    StreamReader readerHtml = new StreamReader(respHttp_.GetResponseStream());

    string szResult_ = readerHtml.ReadToEnd();

    readerHtml.Close();

    return szResult_;
}

 

 

 

 

 

 

 

5. 幾個會用到的介面整理

IHTMLDocument2  簡單來說主要是代表目前這整份html文件

http://msdn.microsoft.com/en-us/library/aa752574(VS.85).aspx

 

IHTMLElement 目前這個階層的標籤元素

http://msdn.microsoft.com/en-us/library/aa752279(VS.85).aspx

 

IHTMLElementCollection 目前這個階層的標籤元素集合

http://msdn.microsoft.com/en-us/library/aa703928(VS.85).aspx

 

 

 

6. 程式碼開發時可能遇到的疑問

Question:

Why are there 4 options to choose from and which one is the correct one to use?
IHTMLElement or IHTMLElement2 or IHTMLElement3 or IHTMLElement4

Answer:

IHTMLElement is the original interface, but as more functionality was added
the 2, 3 and 4 interfaces were created. All are valid, so use the one that
exposes the methods and/or properties that you require.

http://bytes.com/topic/visual-basic-net/answers/386946-ihtmlelement-question