Chinaunix首页 | 论坛 | 博客
  • 博客访问: 39924
  • 博文数量: 11
  • 博客积分: 530
  • 博客等级: 中士
  • 技术积分: 140
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-22 21:56
文章分类

全部博文(11)

文章存档

2010年(1)

2009年(3)

2008年(7)

我的朋友

分类: C/C++

2008-11-25 15:50:48

 本代码是关于 从 ACTIVEX控件 WebBrower 中获取 HTML 和文本。
本代码来自网络,从哪里来不记得了。
  原来代码中有获得HTML,但是我项目中的目的是得到HTML中的文本。料想 自己还要写一个HTML-->TXT
的代码来解决最后的问题。写着写着看到 hr=pElement->get_outerHTML();这个代码 和javascript中的inner***很类似。如是利用自动不起功能居然发现了pElement->get_outerText(&pContent);于是直接省略了
HTML-->TXT的代码。。噢 吔!!!

pBrowse = (CWebBrowser2*)this->GetDlgItem(IDC_EXPLORER3);
    IHTMLDocument2 *pHTMLDocument=NULL;
    if (!(pHTMLDocument = (IHTMLDocument2*)pBrowse->GetDocument()))
        return;
    CComPtr<IHTMLElementCollection> pAllColl;
    HRESULT hr;
    hr=pHTMLDocument->get_all(&pAllColl);
    if(hr==S_OK){
        LONG length=0;
        hr=pAllColl->get_length(&length);
        if(hr==S_OK){
            for(int i=0;i<length;i++){
                VARIANT vIndex,vName;
                vName.vt=vIndex.vt=VT_I4;
                vName.lVal=vIndex.lVal=i;
                CComPtr<IDispatch> pDisp;
                hr=pAllColl->item(vName,vIndex,&pDisp);
                if( hr==S_OK ){
                    CComPtr<IHTMLElement> pElement;
                    hr=pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
                    if( hr==S_OK ){
                        CComBSTR tagName;
                        hr=pElement->get_tagName(&tagName);
                        if(hr==S_OK){
                            CString str(tagName);
                            if(str=="HTML"){
                                CComBSTR pContent;
                                hr=pElement->get_outerText(&pContent);
                                //hr=pElement->get_outerHTML();

                                if(hr==S_OK){

                                    UpdateData(true);
                                    m_text = CString(pContent);
                                    UpdateData(false);
                                    i=length;//以便退出循环

                                }
                                else{//if get_outerHTML failed

                                 MessageBox("can't get html code");
                                }
                            }//else if tagName isnot 'HTML'

                        }//else if get_tagName failed

                    }//else if don't get IHMTLElement interface

                }//if no items

            }
        }//if get_length failed

    }//if get_all failed

    pHTMLDocument->Release();

阅读(6141) | 评论(6) | 转发(0) |
给主人留下些什么吧!~~

chinaunix网友2009-03-14 12:58:29

大哥大,有没有办法读有框架的页面内容啊?

chinaunix网友2009-03-14 12:55:28

在大哥这有所收获啊!

chinaunix网友2009-03-14 12:54:29

if (!(pHTMLDocument = (IHTMLDocument2*)pBrowse->GetDocument())) 应为: if (!(pHTMLDocument = (IHTMLDocument2*)pBrowse->get_Document()))

chinaunix网友2009-03-14 12:53:17

应该是这样的: CWebBrowser2 *pBrowse= (CWebBrowser2*)this->GetDlgItem(IDC_EXPLORER3);

chinaunix网友2009-03-14 12:40:53

老大,有源码共享吗?