在下学习CExplorer期间,想试着获取一个网页上的所有链接,并获得链接的相应属性,比如地址,名称之类,于是写了这样一个代码: CComQIPtr<IHTMLDocument2> spDoc = m_Web.get_Document();
if (NULL == spDoc)
{
return;
} HRESULT hRes;
long nFormCount = 0;
CComBSTR bstrTitle;
CString BSLink;
CComQIPtr<IHTMLElementCollection> spElementCollection;
//取得文档标题
spDoc->get_title(&bstrTitle);
//取得超链接集合
hRes = spDoc->get_links(&spElementCollection);
if (FAILED(hRes))
{
return;
}
//取得超链接数量
spElementCollection->get_length(&nFormCount);
if (FAILED(hRes))
{
return;
} for (long i = 0; i < nFormCount; ++ i)
{
IDispatch *pDisp = NULL;
//取得第 i 项
hRes = spElementCollection->item(CComVariant(i), CComVariant(), &pDisp);
if (FAILED(hRes))
{
continue;
}
//为什么这里指针传递不过来?
CComQIPtr<IHTMLLinkElement> spLinkElement = pDisp; }
结果发现pDisp的值明明是存在的,可CComQIPtr<IHTMLLinkElement> spLinkElement被赋值后却仍然为空,百思不得其解。
在另外的代码中,换一个变量类型就可以正常赋值,比如CComQIPtr<IHTMLFormElement> spFormElement = pDisp。
请问这到底是为什么?有什么方法解决吗?多谢各位!
这是完整的工程文件:
if (NULL == spDoc)
{
return;
} HRESULT hRes;
long nFormCount = 0;
CComBSTR bstrTitle;
CString BSLink;
CComQIPtr<IHTMLElementCollection> spElementCollection;
//取得文档标题
spDoc->get_title(&bstrTitle);
//取得超链接集合
hRes = spDoc->get_links(&spElementCollection);
if (FAILED(hRes))
{
return;
}
//取得超链接数量
spElementCollection->get_length(&nFormCount);
if (FAILED(hRes))
{
return;
} for (long i = 0; i < nFormCount; ++ i)
{
IDispatch *pDisp = NULL;
//取得第 i 项
hRes = spElementCollection->item(CComVariant(i), CComVariant(), &pDisp);
if (FAILED(hRes))
{
continue;
}
//为什么这里指针传递不过来?
CComQIPtr<IHTMLLinkElement> spLinkElement = pDisp; }
结果发现pDisp的值明明是存在的,可CComQIPtr<IHTMLLinkElement> spLinkElement被赋值后却仍然为空,百思不得其解。
在另外的代码中,换一个变量类型就可以正常赋值,比如CComQIPtr<IHTMLFormElement> spFormElement = pDisp。
请问这到底是为什么?有什么方法解决吗?多谢各位!
这是完整的工程文件:
解决方案 »
- vc项目汉化
- 读取文本文件指定一行数据出错,请教!
- 那位前辈有空帮小弟看看这段代码有没有什么错误,为什么总是编译不过去!
- 无法定位序数9336于动态链接库mfc90.dll上???
- 一个简单的返回值问题!
- MSVC6编译opal问题
- 在动态库中如何使用定时器SetTimer或类似的方法,我用的是SetTimer但是有问题,不能在程序内部触发函数
- 请教,股票分析软件是用什么写的?
- 为什么控件窗口子类化后(SetWindowLong后),控件窗口为什么不能自动刷新?
- 求救!用api实现打印预览的步骤,快来救命呀
- 数据库执行出现put_CommandText异常!
- 为什么设置了SetExternalUIHandler,然后SetAmbientDispatch设置的接口的invoke就进不去了。。。也就是说过虑图片过虑不了了
http://msdn.microsoft.com/en-us/library/ms535848(v=vs.85).aspx
看看最下面的,所以应该不是得到你其它链接的,或者你应该得到整个网页源码进行解析
CComPtr<IHTMLLinkElement> spLinkElement;
pDisp->QueryInterface(__uuidof(IHTMLLinkElement), (void**)&spLinkElement);
if (NULL == spDoc)
{
return;
} HRESULT hRes;
long nFormCount = 0;
CComBSTR bstrTitle;
CString BSLink[10];
CComQIPtr<IHTMLElementCollection> spElementCollection;
//取得文档标题
spDoc->get_title(&bstrTitle);
//取得超链接集合
hRes = spDoc->get_links(&spElementCollection);
if (FAILED(hRes))
{
return;
}
//取得超链接数量
spElementCollection->get_length(&nFormCount);
if (FAILED(hRes))
{
return;
} for (long i = 0; i < nFormCount; ++ i)
{
IDispatch *pDisp = NULL;
CComQIPtr<IHTMLElement> spElement;
//取得第 i 项
hRes = spElementCollection->item(CComVariant(i), CComVariant(), &pDisp);
if (FAILED(hRes))
{
continue;
} pDisp->Release(); hRes = pDisp->QueryInterface(IID_IHTMLElement, (void**)&spElement);
if (FAILED(hRes))
{
continue;
} //++ m_Count;
//if (m_Count < 20)
//{
// continue;
//} spElement->get_innerText((BSTR*)&BSLink[0]);
spElement->get_innerHTML((BSTR*)&BSLink[1]);
spElement->get_outerText((BSTR*)&BSLink[2]);
spElement->get_outerHTML((BSTR*)&BSLink[3]); spElement->click();
break;
}
不过这个IHTMLElement貌似有点复杂……请问我如果想改变一个超级链接的属性,比如将它原本的从新窗口打开改为从原有窗口打开,该怎么做呢?
VARIANT VA_Attribute = {0};
CString szTemp = "target";
BSLink[4] = szTemp.AllocSysString();
szTemp = _T("_self");
BSTR BSTemp = szTemp.AllocSysString();
VA_Attribute.vt = VT_BSTR;
VA_Attribute.bstrVal = BSTemp;
//spElement->get_innerText(&BSLink[0]);
//spElement->get_innerHTML(&BSLink[1]);
//spElement->get_outerText(&BSLink[2]);
//spElement->get_outerHTML(&BSLink[3]); spElement->setAttribute(BSLink[4], VA_Attribute);
spElement->click();