代码是关于 从 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();
本文作者:ring8595 来源:http://blog.chinaunix.net/u2/67216/showart_1681923
CIO之家 www.ciozj.com 微信公众号:imciow
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。