3,055
社区成员
发帖
与我相关
我的任务
分享
IShellWindowsPtr m_spSHWinds = NULL;
CComPtr<IDispatch> spDispatch;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComPtr<IHTMLElement> pElement;
long n = 0;
m_spSHWinds->get_Count(&n);
for(long i = 0;i < n;i++)
{
VARIANT va;
va.vt = VT_I4;
va.lVal = i;
IDispatch* spDisp;
m_spSHWinds-> Item(va,&spDisp);
IWebBrowser2* spBrowser;
spDisp-> QueryInterface(IID_IWebBrowser2,(void**)&spBrowser);
IHTMLDocument2* spDoc;
IDispatch* IDispDoc;
spBrowser->get_Document(&IDispDoc);
IDispDoc-> QueryInterface(IID_IHTMLDocument3,(void**)&spDoc3);
if(spDoc3 != NULL)
{
IHTMLElement* element;
if(SUCCEEDED(spDoc3->get_documentElement(&element)))
{
//element->get_b
BSTR body;
if(SUCCEEDED(element->get_innerHTML(&body)))
{
//body中可以获取部分源代码,但是不完整,数据部分取不到
}
}
}
}
void CTestHtmlViewView::OnInitialUpdate()
{
CHtmlView::OnInitialUpdate();
m_strUrl=_T("http://bbs.csdn.net/topics/391960919");
Navigate2(m_strUrl,NULL,NULL);
}
void CTestHtmlViewView::OnDocumentComplete(LPCTSTR lpszURL)
{
CHtmlView::OnDocumentComplete(lpszURL);
CString strUrl=lpszURL;
if (!strUrl.CompareNoCase(m_strUrl)) return;
IDispatch* IDispDoc=this->GetHtmlDocument();
IHTMLDocument2* pDoc;
IDispDoc->QueryInterface(IID_IHTMLDocument2,(void**)&pDoc);
IHTMLElement* elementBody;
IHTMLElement* elementHtml;
pDoc->get_body(&elementBody);
elementBody->get_parentElement(&elementHtml);
BSTR bstr;
elementHtml->get_outerHTML(&bstr);
CString str=bstr;
int len=WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),NULL,0,NULL,NULL);
char* buf=new char[len+1];
WideCharToMultiByte(CP_ACP,0,str,str.GetLength(),buf,len,NULL,NULL);
CFile output;
output.Open(_T("d:\\1.txt"),CFile::modeCreate|CFile::modeWrite);
output.Write(buf,len);
output.Close();
delete[] buf;
}
{
...
if (spDoc3!=NULL)
{
IHTMLElement* elementBody;
IHTMLElement* elementHtml;
spDoc3->get_body(&elementBody);
elementBody->get_parentElement(&elementHtml);
BSTR bstr;
elementHtml->get_outerHTML(&bstr);
...
}
}