CHtmlView获取网页所有元素的连接和名字

来源:互联网 发布:java管理系统开发 编辑:程序博客网 时间:2024/05/07 06:48
BOOL   MyDlg::GetAllLinks(CMyHtmlView   *pView) { IHTMLDocument2   *   pDoc   =   NULL; IHTMLElementCollection     *   pAllElem   =   NULL; IHTMLElement   *pElem   =   NULL; IHTMLAnchorElement   *   pAnchor   =   NULL; IHTMLElement*   ptext; long   p; BSTR   bstrSrc; VARIANT   name; name.vt   =   VT_I4; try{ pDoc   =   (IHTMLDocument2*)(pView-> GetHtmlDocument()); if(pDoc!=NULL) { pDoc-> get_links(&pAllElem); if(pAllElem!=NULL) { pAllElem-> get_length(&p); if(p> 0) { for(int   i=0;   i <p;   i++) { name.lVal   =   i; if(pAllElem-> item(name,   name,   (LPDISPATCH*)&pElem)==S_OK) { if(pElem   !=   NULL) { pElem-> QueryInterface(&pAnchor); if(pAnchor!=NULL) { pAnchor-> get_href(&bstrSrc); CString   sHref(bstrSrc);     //url SysFreeString(bstrSrc); pAnchor-> QueryInterface(&ptext); ptext-> get_innerText(&bstrSrc); CString   sText(bstrSrc);   //text SysFreeString(bstrSrc); ptext-> Release(); pAnchor-> Release(); pAnchor   =   NULL; } pElem-> Release(); pElem   =   NULL; } } } } } } }catch(...){} try{ if(pDoc   !=   NULL) pDoc-> Release(); if(pAllElem   !=   NULL) pAllElem-> Release(); }catch(...){} return   TRUE; }