我知道如何使用 IHTMLDocument2 接口(interface)(通过调用 get_body 成员函数)获取文档的 HTML 正文。
但是我怎样才能得到头部呢? IHTMLDocument2接口(interface)中没有这个函数吗?
最佳答案
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 的元素。
希望这会有所帮助。
关于c++ - 从 IE DOM 获取 HTML Head,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8435230/