怎样获得某一当前打开的网页中的所有超链接的URL?
在一个对话框中,怎样获得webbrowser组件已经打开的网页中的所有超链接的地址?如下:
m_ie.Navigate( "http://www.hao123.com/ ",NULL,NULL,NULL,NULL);
//接下来怎样获得http://www.hao123.com/网页中的超链接的地址
[解决办法]
看了下SDK文档 没有试过
大约可以通过HRESULT IHTMLDocument2::get_links(IHTMLElementCollection **p);
获得连接元素的集合 然后从IHTMLElementCollection::item(VARIANT name,VARIANT index, IDispatch **pdisp)获得每个连接对象的IDispatch指针
然后应该可以查询IHTMLLinkElement/IHTMLLinkElement2/IHTMLLinkElement3
没有验证过
[解决办法]
IDispatch *pDisp = NULL;
pDisp = m_WebBrowser.GetDocument();
IHTMLDocument2 *pHtmlDocument2 = NULL;
pDisp-> QueryInterface(IID_IHTMLDocument2, (void **)&pHtmlDocument2);
IHTMLElementCollection *pCol;
pHtmlDocument2-> get_links(&pCol);
if (pCol == NULL) return;
long p = 0;
int nCol = pCol-> get_length(&p);
if(p == 0 ) return;
for(int i = 0; i < p; i ++)
{
IDispatch *pDisp = NULL;
pCol-> item(CComVariant(i), CComVariant(i), &pDisp);
IHTMLElement *pElem;
pDisp-> QueryInterface(IID_IHTMLElement, (void **)&pElem);
BSTR pBstr;
pElem-> get_innerText(&pBstr);
IHTMLAnchorElement *pLinkElem;
pDisp-> QueryInterface(IID_IHTMLAnchorElement, (LPVOID*)&pLinkElem);
BSTR pLink;
pLinkElem-> get_href(&pLink);
CString str=BSTR2CString(pLink);//超链接的url
CString str1=BSTR2CString(pBstr);//超链接显示的字
}
[解决办法]
需要Mshtml.h