当前位置:首页 >> 编程开发 >> Visual C++ >> 内容

如何分析网页元素并进行相应处理

时间:2010/9/14 作者:平凡之路 来源:xuhantao.com 浏览:

如何分析网页元素,然后进行相应处理,比如填表,递交等等

#include <Mshtml.h>
AnsiString __fastcall TForm3::Submit(void)
{
IHTMLDocument2 *pHTMLDoc = NULL;
LPDISPATCH pDisp = NULL;
pDisp =CppWebBrowser1->Document;
if(pDisp)
  {
  if (SUCCEEDED(pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pHTMLDoc)))
   {
   pDisp->Release();
   IHTMLElementCollection *pelement;
   if(pHTMLDoc->get_forms(&pelement)!=S_OK)
    {
    pHTMLDoc->Release();
    pDisp->Release();
    pelement->Release();
    return "1发送失败";
    }
   pDisp->Release();
   tagVARIANT a,i;
   a.vt=VT_UI4;
   a.lVal=0;//这个值为你所要填写的表单在整个网页中的顺序,0为第一个表单
   if(pelement->item(a,i,&pDisp)!=S_OK)
    {
    pelement->Release();
    pDisp->Release();
    return "2发送失败";
    }
   pelement->Release();
   IHTMLFormElement* pFormElem=NULL;
   if(pDisp)
    {
    if(!SUCCEEDED(pDisp->QueryInterface(IID_IHTMLFormElement,(LPVOID*)&pFormElem)))
     {
     pFormElem->Release();
     pDisp->Release();
     return "3发送失败";
     }
    }
   else
    {
    return "31发送失败";
    }
   pDisp->Release();
   LPDISPATCH pDisp_msg = NULL;
   tagVARIANT phone,msg,empty;
   phone.vt=VT_UI4;
   phone.lVal=0;//这个值为Input框在表单中的顺序
   msg.vt=VT_UI4;
   msg.lVal=1;//这个值为Text文本框在表单中的顺序
   if((pFormElem->item(phone,empty,&pDisp)!=S_OK)||(pFormElem->item(msg,empty,&pDisp_msg)!=S_OK))
    {
    pFormElem->Release();
    if(pDisp)
     pDisp->Release();
    if(pDisp_msg)
     pDisp_msg->Release();
    return "4发送失败";
    }
   IHTMLInputTextElement* phoneElem=NULL;
   IHTMLTextAreaElement* msgElem;
   if(!pDisp||!pDisp_msg)
    return "11失败";
   if (!SUCCEEDED(pDisp->QueryInterface(IID_IHTMLInputTextElement, (void**)&phoneElem))||(!SUCCEEDED(pDisp_msg->QueryInterface(IID_IHTMLTextAreaElement, (void**)&msgElem))))
    {
    if(phoneElem)
     phoneElem->Release();
    if(msgElem)
     msgElem->Release();
    if(pDisp)
     pDisp->Release();
    if(pDisp_msg)
     pDisp_msg->Release();
    return "5发送失败";
    }
   WideString s_phone,s_msg;
   s_phone="888888"
   s_msg="你好";
   phoneElem->put_value(s_phone.c_bstr());//填写
   msgElem->put_value(s_msg.c_bstr());//填写
   pFormElem->submit();//提交
   phoneElem->Release();
   msgElem->Release();
   pDisp->Release();
   pDisp_msg->Release();
   pFormElem->Release();
   return "发送成功";
   }
  }
return "发送失败";
}

相关文章
  • 没有相关文章
  • 徐汉涛(www.xuhantao.com) © 2024 版权所有 All Rights Reserved.
  • 部分内容来自网络,如有侵权请联系站长尽快处理 站长QQ:965898558(广告及站内业务受理) 网站备案号:蒙ICP备15000590号-1