反爬比较厉害的网站,用WebBrowser来获取网页源码,也可使用开源的浏览器来实现。
#include <MsHTML.h>
#include <atlconv.h>
void CMFCApplication1Dlg::DocumentCompleteExplorer1(LPDISPATCH pDisp, VARIANT* URL)
{
CComDispatchDriver lpDisp = m_web.get_Application();
if (lpDisp == pDisp)
{
//把地址更新到地址栏
m_szUrl = URL->bstrVal;
UpdateData(FALSE);
//获取网页源码
//IHTMLDocument get_Script();
//CComQIPtr <IHTMLDocument> pDocument1(m_web.get_Document());
CComQIPtr <IHTMLDocument2> pDocument2(m_web.get_Document()); //get_frames()
//IHTMLDocument3 get_parentDocument() getElementsByTagName() getElementsByName() getElementById()
//CComQIPtr <IHTMLDocument3> pDocument3(m_web.get_Document());
//CComQIPtr <IHTMLDocument6> pDocument6(m_web.get_Document());
//IHTMLDocument7 get_head() getElementsByTagNameNS() getElementsByClassName()
//CComQIPtr <IHTMLDocument7> pDocument7(m_web.get_Document());
CComQIPtr <IHTMLElement> pBody;
CComQIPtr <IHTMLElement> pHTML;
HRESULT hRet = pDocument2->get_body(&pBody); //获得body
if (FAILED(hRet) || !pBody) return;
BSTR bszHtml, bszHtml2;
//pBody->get_outerHTML(&bszHtml); //获得body中的html
CComQIPtr <IHTMLElement> pEle;
pBody->get_parentElement(&pEle); //获得body的父元素
pEle->get_outerHTML(&bszHtml); //获得页面完整html
pEle->get_innerHTML(&bszHtml2); //部分html,head+body
//pEle->get_innerText(&bszHtml2); //获得页面所有可见文本
//TRACE(_T("%Ls"), bszHtml2);
CString szTemp;
szTemp.Format(_T("%Ts"), bszHtml);
CString filename = _T("z:\\test.html");
CFile oFile;
if (oFile.Open(filename, CFile::modeCreate | CFile::modeWrite))
oFile.Write(szTemp, szTemp.GetLength() * sizeof(TCHAR));
//oFile.Flush();
oFile.Close();
//pDocument2->get_all(&pEle);
//pEle->tags();
/*pDocument7->get_head(&pHTML);
pHTML->get_innerHTML(&bstr);*/
}
}