再升级版说明:通过frame的get_location属性,指定frame来获取其元素,减少递归和循环,减少循环和递归,基于效能提升门户生产地址获取主叫,可从6s压缩到1s,耗时在于指定frame所有元素循环上。

1.独立代码

//--------------获取效能提升门户主叫---------------------------------------//
#include <atlbase.h>
#include <mshtml.h>
#include <winuser.h>
#include <comdef.h>
#include <string.h>
void EnumIE(void);//处理网页
void EnummiddleFrame(IHTMLDocument2 * pIHTMLDocument2);//处理框架
void EnumleftFrame(IHTMLDocument2 * pIHTMLDocument2);//处理框架
CComModule _Module;  //使用CComDispatchDriver ATL的智能指针,此处必须声明
#include <atlcom.h>
void EnumAllElement(IHTMLDocument2 * pIHTMLDocument2);//获取网页内元素
CString     glb_strCaller;//全局主叫

void EnumIE(void)  
{
 CComPtr<IShellWindows> spShellWin;  
 HRESULT hr=spShellWin.CoCreateInstance(CLSID_ShellWindows);  
 if (FAILED(hr))  
 {  
  return;  
 }

long nCount=0;    //取得浏览器实例个数(Explorer和IExplorer)  
 spShellWin->get_Count(&nCount);  
 if (0==nCount)  
 {  
   return;  
 }

for(int i=0; i<nCount; i++)  
 {  
  CComPtr<IDispatch> spDispIE;  
  hr=spShellWin->Item(CComVariant((long)i), &spDispIE);  
  if (FAILED(hr)) continue;
 
  CComQIPtr<IWebBrowser2>spBrowser=spDispIE;  
  if (!spBrowser) continue;
 
  CComPtr<IDispatch> spDispDoc;  
  hr=spBrowser->get_Document(&spDispDoc);  
  if (FAILED(hr)) continue;
 
  CComQIPtr<IHTMLDocument2>spDocument2 =spDispDoc;  
  if (!spDocument2) continue;

//Modify by Fang jiansheng 2011-04-02
  //*******************************************************************************
  CString cIEUrl_Filter;  //设置的URL(必须是此URL的网站才有效);
  cIEUrl_Filter="http://172.20.33.130:8082/csp/"; //效能提升门户过滤的网址   
  //*******************************************************************************

CComBSTR IEUrl;
  spBrowser->get_LocationURL(&IEUrl);
  CString cIEUrl_Get;     //从机器上取得的HTTP的完整的URL;
  cIEUrl_Get=IEUrl;
  cIEUrl_Get=cIEUrl_Get.Left(cIEUrl_Filter.GetLength()); //截取前面N位

if (strcmp(cIEUrl_Get,cIEUrl_Filter)==0)
  {
   // 程序运行到此,已经找到了IHTMLDocument2的接口指针
   EnummiddleFrame(spDocument2);
  }   
  }  
}

//在框架内获取主叫
void EnumAllElement(IHTMLDocument2 * pIHTMLDocument2) //枚举所有字段
{
 if (!pIHTMLDocument2) return;    
 HRESULT   hr;  
 CComQIPtr<IHTMLElementCollection> spAllElement;
 hr=pIHTMLDocument2->get_all(&spAllElement);//获取所有网页内所有元素
 if (FAILED(hr))  return;

long nLength = 0;
 spAllElement->get_length (&nLength);
 for (int i = 0; i < nLength; i++)
 {
        CComPtr<IDispatch> pDisp;
  hr = spAllElement->item(COleVariant((long)i),COleVariant((long)0),&pDisp); //获取单个元素
  if(SUCCEEDED(hr))
  {
   //CComQIPtr <IHTMLElement, &IID_IHTMLElement> pElement(pDisp);
   CComQIPtr<IHTMLElement, &IID_IHTMLElement> pElement;
   pDisp->QueryInterface(&pElement);
   BSTR bId;
   pElement->get_id(&bId);//可以获取其他特征,根据具体元素而定
   CString strId=bId;
   if(!strId.IsEmpty() && strId=="callNo")//根据id是主叫号码获取值或作其他处理
   {
    IHTMLInputTextElement* input;
    pDisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&input);
    BSTR bVal;
    input->get_value(&bVal);
    if(bVal==NULL) glb_strCaller="";
    else glb_strCaller=bVal;
    break;
   }
  }
 }
}

void EnumleftFrame(IHTMLDocument2 * pIHTMLDocument2)
{
 if (!pIHTMLDocument2) return;
 HRESULT   hr;     
 CComPtr<IHTMLFramesCollection2> spFramesCollection2;  
 pIHTMLDocument2->get_frames(&spFramesCollection2); //取得框架frame的集合  
   
 long nFrameCount=0;        //取得子框架个数  
 hr=spFramesCollection2->get_length(&nFrameCount);
 if (FAILED(hr)|| 0==nFrameCount) return;  
 for(long i=0; i<nFrameCount; i++)  
 { 
  CComVariant vDispWin2; //取得子框架的自动化接口  
  hr = spFramesCollection2->item(&CComVariant(i), &vDispWin2);
  if (FAILED(hr)) continue;
  CComQIPtr<IHTMLWindow2>spWin2 = vDispWin2.pdispVal;
  //CComQIPtr<IHTMLFrameElement, &IID_IHTMLFrameElement> pFrmElement=vDispWin2.pdispVal;
  if (!spWin2) continue; //取得子框架的   IHTMLWindow2   接口
  CComPtr <IHTMLLocation> spLoc;
  spWin2->get_location(&spLoc);//获取frame的页面地址
  BSTR bHref;
  spLoc->get_href(&bHref);//获取链接地址
        CString strHref=bHref;
  if(!strHref.IsEmpty() && strHref=="http://172.20.33.130:8082/csp/bsm/leftFrame.action")
  { 
   //效能提升门户中间框架leftFrame的页面地址 
   CComPtr <IHTMLDocument2> spDoc2;  
   spWin2->get_document(&spDoc2); //取得子框架的   IHTMLDocument2   接口
   EnumAllElement(spDoc2);//获取效能提升门户主叫
   break;
  }  
 }  
}

void EnummiddleFrame(IHTMLDocument2 * pIHTMLDocument2)
{
 if (!pIHTMLDocument2) return;
 HRESULT   hr;     
 CComPtr<IHTMLFramesCollection2> spFramesCollection2;  
 pIHTMLDocument2->get_frames(&spFramesCollection2); //取得框架frame的集合  
   
 long nFrameCount=0;        //取得子框架个数  
 hr=spFramesCollection2->get_length(&nFrameCount);
 if (FAILED(hr)|| 0==nFrameCount) return;  
 for(long i=0; i<nFrameCount; i++)  
 { 
  CComVariant vDispWin2; //取得子框架的自动化接口  
  hr = spFramesCollection2->item(&CComVariant(i), &vDispWin2);
  if (FAILED(hr)) continue;
  CComQIPtr<IHTMLWindow2>spWin2 = vDispWin2.pdispVal;
  //CComQIPtr<IHTMLFrameElement, &IID_IHTMLFrameElement> pFrmElement=vDispWin2.pdispVal;
  if (!spWin2) continue; //取得子框架的   IHTMLWindow2   接口
  CComPtr <IHTMLLocation> spLoc;
  spWin2->get_location(&spLoc);//获取frame的页面地址
  BSTR bHref;
  spLoc->get_href(&bHref);//获取链接地址
        CString strHref=bHref;
  if(!strHref.IsEmpty() && strHref=="http://172.20.33.130:8082/csp/mif/middleFrame.action")
  { 
   //效能提升门户中间框架middleFrame的页面地址
   CComPtr <IHTMLDocument2> spDoc2;  
   spWin2->get_document(&spDoc2); //取得子框架的   IHTMLDocument2   接口
   EnumleftFrame(spDoc2);//获取效能提升门户左边框架leftFrame
   break;
  }  
 }  
}

//-----------结束---------------------//

2.执行代码:

void CDemoDlg::OnOK()
{
 // TODO: Add extra validation here
 ::CoInitialize(NULL); //初始化COM
     EnumIE();             //枚举浏览器      
     ::CoUninitialize();   //释放COM
 //CDialog::OnOK();
}

vc采集网页内指定frame框架下所有元素-再升级版相关推荐

  1. vc采集网页内frame框架下所有元素(不指定具体table/form)-升级版

    升级版说明:增加对获取指定控件的判断,利用标志bGetCaller,减少循环和递归,基于效能提升门户生产地址获取主叫,可从60s压缩到6s 1.独立代码 #include <atlbase.h& ...

  2. vc采集网页内frame框架下所有元素(不指定具体table/form)

    1.独立代码 //-----------开始---------------------// #include <atlbase.h> #include <mshtml.h> # ...

  3. vc采集网页内所有元素(不指定具体table/form/frame)

    1.独立代码 //-----------开始---------------------// #include <atlbase.h> #include <mshtml.h> # ...

  4. VC采集网页所有表单域

    1.独立代码 //-----------开始---------------------// #include <atlbase.h> #include <Mshtml.h> # ...

  5. vc获取网页内table

    1.独立代码: //-----------开始---------------------// #include <atlbase.h> #include <mshtml.h> ...

  6. ThinkPHP5 采集网页的指定内容

    荆轲刺秦王 因业务需求,需要做一个网页的信息采集功能.这个网页就是安居客的新房的列表页. 第一步:一开始,我用最基本的采集,采集一点很基本的内容,就是网页 html 的的<title>标签 ...

  7. WEB代码:内嵌框架iframe、frame框架

    1.内嵌框架iframe <iframe src="https://www.baidu.com/" frameborder="0" width=" ...

  8. VC 在经典 MFC 框架下使用真彩色工具栏

    VS2008 sp1 以后有了 BCG 的包,可以使用 CMFCToolbar 来加载真彩色工具栏.但是在经典 MFC 框架下,默认还只是 16 色的工具栏,通过下面的方法就可以加载真彩工具栏了.直接 ...

  9. Arduino框架下ESP32+合宙1.54“ 电子墨水屏(e-paper)驱动显示示例

    Arduino框架下ESP32+合宙1.54" 电子墨水屏(e-paper)驱动显示示例 显示效果展示; 合宙1.54" 电子墨水屏 有关合宙1.54"电子墨水屏的介绍资 ...

最新文章

  1. python 函数调用列表,函数调用列表的Python oneliner
  2. 【c语言】蓝桥杯基础练习 数列特征
  3. 【Android基础】趣谈Intent
  4. pybind 编码h264
  5. 大数据时代,一名优秀的开发者应具备怎样的特质?
  6. 编写你的第一个 Django 应用,第 7 部分
  7. Scrapy源码阅读分析_2_启动流程
  8. StringUtils详细介绍
  9. 微课|中学生可以这样学Python(7.3.3节):成员方法、类方法、静态方法
  10. 【Android开发】消息提示框与对话框-使用AlertDialog创建对话框
  11. 蚂蚁金服技术专家:mPaaS是如何打造“最懂用户”的App的?
  12. 英国云主机节点是欧美五大节点之一
  13. 从零玩转第三方登录之QQ登录
  14. USB3300速度调试
  15. Openstack版本查看
  16. 阿里云服务器续费坑啊早知道不买了
  17. 国际贸易术语解释通则(DEQ 目的港码头交货(……指定目的港))
  18. 邱锡鹏《神经网络与深度学习》第一章 绪论
  19. [分享] Hyper-V 安装Win7激活后黑屏
  20. 邮件常见异常:javax.mail.MessagingException: Could not connect to SMTP host

热门文章

  1. python聚类分析如何确定分类个数_Python数据挖掘—聚类—KMeans划分法
  2. html单击按钮时弹出输入框,点击按钮弹出模态框的一系列操作代码实例
  3. Redis cluster日常管理【二】
  4. Windows服务器下升级PHP版本的方法
  5. 分享android开发过程中用到的一些开源框架
  6. 【H.264/AVC视频编解码技术具体解释】十三、熵编码算法(4):H.264使用CAVLC解析宏块的残差数据...
  7. Linear regression with one variable算法实例讲解(绘制图像,cost_Function ,Gradient Desent, 拟合曲线, 轮廓图绘制)_矩阵操作...
  8. XSLT 与 Java集成常见技术关键点
  9. 第二百九十、一、二天 how can I 坚持
  10. Dollar Dayz poj3181