悉野小楼

导航

MFC webbrowser读取文档的meta分析

IDispatch* pDisp = NULL;
IDispatch* pDisp2 = NULL;
IHTMLDocument2 *pHtmlDoc2 = NULL;
IHTMLElementCollection* pCol;
IHTMLMetaElement *pMeta = NULL;
BSTR bUrl = NULL;
BSTR bstr = NULL; 
try {
    if ((pDisp = m_WebBrowser.get_Document()) &&
        SUCCEEDED(pDisp->QueryInterface(IID_IHTMLDocument2,(PVOID*)&pHtmlDoc2)) 
        && SUCCEEDED(pHtmlDoc2->get_all( &pCol)) && pCol
        )
    {
        //检查域名是www.test.com或www.test2.com的域名下, meta标签是否有myflag标记, 如果有则认为是正确的网页bIsGoodWebPage为TRUE
        pHtmlDoc2->get_URL(&bUrl);
        if(wcsstr((const wchar_t*)bUrl, L"www.test.com") == NULL 
            && wcsstr((const wchar_t*)bUrl, L"www.test2.com") == NULL)
        {
            bIsGoodWebPage = TRUE;
        }
        else
        {
            long eleLen = 0;
            pCol->get_length(&eleLen);
            IHTMLMetaElement* pMetaElement = NULL;
            VARIANT varIndex;
            HRESULT hr;
            for( int i=0; i< eleLen; i++)
            {
                VARIANT var2;
                varIndex.vt = VT_UINT;
                varIndex.lVal = i;
                VariantInit( &var2 );
                hr = pCol->item(varIndex, var2, &pDisp2);
                hr  = pDisp2->QueryInterface(IID_IHTMLMetaElement,(void**)&pMeta);
                if( hr == S_OK)
                {
                    if( SUCCEEDED(pMeta->get_httpEquiv(&bstr)) )
                    {
                        if(!wcscmp((const wchar_t*)bstr,L"keywords"))
                        {
                            bstr = NULL;
                            if(SUCCEEDED(pMeta->get_content(&bstr)))
                            {
                                if(!wcscmp((const wchar_t*)bstr, L"myflag"))
                                {
                                    bIsGoodWebPage = TRUE;
                                    break;
                                }
                            }
                        }    
                    }
                }
            }
        }
    }
}
catch (...)
{
    WBTRACE("Error\r\n");
}
SAFE_RELEASE(pMeta);
SAFE_RELEASE(pDisp2);
SAFE_RELEASE(pHtmlDoc2);
SAFE_RELEASE(pDisp);

 

posted on 2015-02-04 13:50  悉野  阅读(443)  评论(0编辑  收藏  举报