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;
}CHtmlView获取网页所有元素的连接和名字
最新推荐文章于 2026-06-21 09:53:41 发布
本文提供了一段使用C++实现的代码片段,用于从HTML文档中获取所有链接,包括链接的URL和文本内容。

1504

被折叠的 条评论
为什么被折叠?



