Delphi 怎样使用 mshtml 解析 HTML代码(不要依赖 Webbrowse 控件的)

解决方案 »

  1.   

    mshtml是一组由IE提供的DOM接口,无法离开IE/WebBrowser控件使用。
    你可以使用一个隐藏的WebBrowser来获得IE的HTML解析功能,或者考虑其他的HTML解析器实现,可以在Google上搜索"HTML解析器"找到一些有用信息。
      

  2.   

    其接口仍就是IE核心,不过可以用没有UI的TInternetExplorer的
      

  3.   

    var
       doc:IHTMLDocument2;
       hh:String;begin
       hh := '<font color="red">字体</font>';
       doc := CoHTMLDocument.Create as IHTMLDocument2;
       
       //我创建了 doc 实例,想用 doc 来解析 hh,取出 hh 的innerHTML 和 属性 color
       //怎么做?急用谢谢了
      

  4.   

    使用mshtml解析HTML源文件,不下载 
    http://program.sdoq.com/show/68305
      

  5.   

    贴一段代码或许对你有用 
    void       OnLogin()       
    {   
    _variant_t       v;   
    HRESULT       hr;   
    VARIANT       id,       index;   
    CComPtr   <IDispatch>       spDispatch;   
    CComQIPtr   <IHTMLDocument2,       &IID_IHTMLDocument2>       pDoc2;   
    CComQIPtr   <IHTMLElement,       &IID_IHTMLElement>       pElement;   
    CComQIPtr   <IHTMLElementCollection,&IID_IHTMLElementCollection>       pElementCol;
    CComPtr   <IHTMLInputTextElement>       pInputTextElement;
    if       (m_spSHWinds       ==       NULL)       {   
    hr       =       m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows));   
       if       (FAILED(hr)){   
               MessageBox(   "Failed   ");   
               CoUninitialize();   
        }   
    }   
    if       (m_spSHWinds)       {   
           long       n=0;   
           m_spSHWinds->   get_Count(&n);       //得到浏览器的个数   
               //遍历每一个浏览器   
             for       (long       i       =       0;       i       <       n;       i++)       {   
             CComPtr   <IDispatch>       spDisp;   
             v       =       (long)i;  
             spDisp       =m_spSHWinds->   Item(&v);   
             SHDocVw::IWebBrowser2Ptr       spBrowser(spDisp);               //生成一个IE窗口的智能指针   
              if       (spBrowser)           { 
        //获取IHTMLDocument2接口   
                 if       (SUCCEEDED(spBrowser->   get_Document(       &spDispatch)))   
                     pDoc2       =       spDispatch;   
                if(pDoc2!=NULL)       {   
                     //获取所有元素的集合   
                       if(SUCCEEDED(pDoc2->   get_all(&pElementCol))){   
                     long       p=0;   
                     if(SUCCEEDED(pElementCol->   get_length(&p)))                   {       
                          if(p!=0){
                                //遍历所有元素           
                                    for(long       i=0;i   <p;i++){
                                    V_VT(&id)       =       VT_I4;   
                                    V_I4(&id)       =       i;   
                                    V_VT(&index)       =       VT_I4;   
                                    V_I4(&index)       =       0;   
                          if(SUCCEEDED(pElementCol->   item(id,index,       &spDispatch)))       {
    //应该就这附近的几行错误吧   
    if(SUCCEEDED(spDispatch->   QueryInterface(IID_IHTMLElement,(void**)&pElement)))       {           //应该就这附近的几行错误吧   
        
            if(SUCCEEDED(pElement->   QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputTextElement)))       {               
    CComBSTR       strName(   "csdn   ");           //用户名   
    CComBSTR       strPwd(   "123456   ");               //密码   
    CComBSTR       type;                                                       //输入框类型   
    pInputTextElement->   get_type(&type);//获取输入框类型   
    CString       strtype(type);   
    strtype.MakeUpper();   
                                                                                            if       (lstrcmpi(strtype,       _T(   "TEXT   "))       ==       0)       {   
                                                                                                                                                                                                            pInputTextElement->   put_value(strName);//设置文本框的值(用户名)       
                                }   
                                                                if       (lstrcmpi(strtype,       _T(   "PASSWORD   "))       ==       0)       {   
                                                                                                                                                                                                            pInputTextElement->   put_value(strPwd);//设置文本框的值(密码)   
                                                                        }               }                                                                       }   
                        }   
    }   
                }   
    }   
                    }   
                }   
                
                        }   
                }   
                }   
    }       the       following       code       will       click       submit       button       of       html       page       in       IE,       modify       it       to       click       other       button.       
        ///////////////////////////////////////////////////////////////////////////////////////       
        //click       submit       button       of       IE       window       
        //If       it       works,       it       is       written       by       masterz,otherwise       I       don't       
        //know       who       writes       it^_^       
        ///////////////////////////////////////////////////////////////////////////////////////       
        void       CGetIESrcDlg::NavigateToUrl()       
        {       
        //       Import       the       following       files       in       your       stdafx.h       
        //   #import       <mshtml.tlb>       //       Internet       Explorer       5       
        //   #import       <shdocvw.dll>       
        //           Refer       to       "Connect       to       Internet       Explorer       Instances,       From       your       own       Process.       "       in       www.codeguru.com       
        SHDocVw::IShellWindowsPtr       m_spSHWinds;       
        CoInitialize(NULL);       
        if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows))       ==       S_OK)       
        {       
        IDispatchPtr       spDisp;       
        long       nCount       =       m_spSHWinds-> GetCount();       
        for       (long       i       =       0;       i       <       nCount;       i++)       
        {       
        _variant_t       va(i,       VT_I4);       
        spDisp       =       m_spSHWinds-> Item(va);       
        SHDocVw::IWebBrowser2Ptr       spBrowser(spDisp);       
        if       (spBrowser       !=       NULL)       
        {       
        IDispatchPtr       spDisp;       
        if(spBrowser-> get_Document(&spDisp)       ==       S_OK       &&       spDisp!=       0       )       
        {       
        MSHTML::IHTMLDocument2Ptr       spHtmlDocument(spDisp);       
        MSHTML::IHTMLElementPtr       spHtmlElement;       
        if(spHtmlDocument==NULL)       
        continue;       
        spHtmlDocument-> get_body(&spHtmlElement);       
        if(spHtmlDocument==NULL)       
        continue;       
        HRESULT       hr;       
        MSHTML::IHTMLElementCollection*       pColl=NULL;       
        hr=spHtmlDocument-> get_all(&pColl);       
        if(pColl!=NULL&&SUCCEEDED(hr))       
        {       
        MSHTML::IHTMLElement*       pElem=NULL;       
        _variant_t       index;       
        index.vt=VT_I4;       
        index.intVal=0;       
        _variant_t       name("Submit");       
        IDispatchPtr       disp;       
        disp=pColl-> item(name,index);       
        if(disp==NULL)       
        hr=E_FAIL;       
        else       
        {       
        hr=disp-> QueryInterface(&pElem);       
        }       
        if       (SUCCEEDED(hr)&&       pElem       !=       NULL)       
        {       
        //       
        BSTR       bstrhtml;       
        pElem-> get_outerHTML(&bstrhtml);       
        CString       str(bstrhtml);       
        AfxMessageBox(str);       
        pElem-> click();       
        pElem-> Release();       
        }       
        pColl-> Release();       
        }       
        }       
            
        }       
        }       
            
        }       
        else       {       
        AfxMessageBox("Shell       Windows       interface       is       not       avilable");       
        }       
        CoUninitialize();       
        }