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

文档标题:如何分析网页元素并进行相应处理 
关 键 字:分析网页元素,MShtml.h,CppWebBrowser1->Document,LPDISPATCH,IHTMLDocument2 



--------------------------------------------------------------------------------
 
如何分析网页元素并进行相应处理
作者: Friecin ,如转载请保证本文档的完整性,并注明出处。
如何分析网页元素,然后进行相应处理,比如填表,递交等等


#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 "发送失败";
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值