我知道如何使用IHTMLDocument2接口(interface)(通过调用get_body成员函数来获取文档的HTML正文。

但是,我该如何获得头? IHTMLDocument2界面中没有这样的功能?

最佳答案

    CComPtr<IHTMLDocument2> htmlDocument;
    CComPtr<IHTMLElementCollection> elementCollection;

    htmlDocument->get_all(&elementCollection);
    for (long i=0;i<numberOfElements;i++)
    {
        _variant_t index = i;
        CComPtr<IHTMLElement> htmlElem;
        CComPtr<IDispatch> htmlElemDisp;
        hResult = elementCollection->item( index,index ,(IDispatch **) &htmlElemDisp );
        if (FAILED(hResult) || (!(htmlElemDisp)))
        {
            continue;
        }
        hResult = htmlElemDisp->QueryInterface( IID_IHTMLElement ,(void **) &htmlElem);
        if (FAILED(hResult) || (!(htmlElem)))
        {
            continue;
        }
        hResult = htmlElem->get_tagName(&buffer);

        if (FAILED(hResult) || (!(buffer)))
        {
            continue;
        }
        if (_wcsicmp(buffer,L"HEAD")==0)
        {
         // your code here
        }
}

另外,您可以使用IHTMLDocument2* htmlDocument代替CComPtr<IHTMLDocument2> htmlDocument
主要思想是获取文档中的所有元素,对其进行迭代,然后找到具有tagName HEAD的元素。
希望这可以帮助。

10-05 20:40
查看更多