现在的位置: 首页 > 综合 > 正文

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

2013年02月05日 ⁄ 综合 ⁄ 共 2284字 ⁄ 字号 评论关闭

文档标题:如何分析网页元素并进行相应处理 
关 键 字:分析网页元素,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 "发送失败";

抱歉!评论已关闭.