描述:
已有IWebBrowser2指针,怎样获取网页中的文本信息,修改后再设置回去,说的明白点,就是要对一个网页进行翻译
解决方案1:
hehe。
蒋晟给的东西就是好
http://dev.csdn.net/article/18/18465.shtm
不过这样你要自动检测网页的代码页和分析网页结构
只获得文本的话用document.body.innerhtml就好了
void CDoHTMLAppDlg::Spy()
{
SHDocVw::IShellWindowsPtr m_spSHWinds;
CComPtr<IDispatch> spDispatch;
CComQIPtr<IHTMLDocument2, &IID_IHTMLDocument2> pDoc2;
CComPtr<IHTMLElement> pElement;
CComPtr<IHTMLElementCollection> pElementCol;
CComPtr<IHTMLFormElement> pFormElement;
CComPtr<IHTMLInputTextElement> pInputElement;
if(m_spSHWinds==NULL)
{
if(m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows))!=S_OK)
{
AfxMessageBox("失败...",MB_ICONINFORMATION);
CoUninitialize();
}
}
if(m_spSHWinds)
{
int n=m_spSHWinds->GetCount();
for(int i=0;i<n;i++)
{
_variant_t v=(long)i;
IDispatchPtr spDisp=m_spSHWinds->Item(v);
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);//生成一个IE窗口的智能指针
if(spBrowser)
{
//
VARIANT id,index;
if(SUCCEEDED(spBrowser->get_Document(&spDispatch)))
pDoc2 = spDispatch;
if(pDoc2!=NULL)
{
if(SUCCEEDED(pDoc2->get_forms(&pElementCol)))
{
long p=0;
if(SUCCEEDED(pElementCol->get_length(&p)))
if(p!=0)
{
for(long i=0;i<=(p-1);i++)
{
V_VT(&id)=VT_I4;
V_I4(&id)=i;
V_VT(&index)=VT_I4;
V_I4(&index)=0;
if(SUCCEEDED(pElementCol->item(id,index,&spDispatch)))
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement)))
{
long q=0;
if(SUCCEEDED(pFormElement->get_length(&q)))
for(long j=0;j<=(q-1);j++)
{
V_VT(&id)=VT_I4;
V_I4(&id)=j;
V_VT(&index)=VT_I4;
V_I4(&index)=0;
if(SUCCEEDED(pFormElement->item(id,index,&spDispatch)))
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement)))
{
CComBSTR value;
CComBSTR type;
pInputElement->get_type(&type);
CString strtype(type);
strtype.MakeUpper();
if(strtype.Find("TEXT")!=-1)
{
pInputElement->get_value(&value);
CString str(value);
if(!str.IsEmpty())
{
m_List.AddString(_bstr_t("文本信息:")+_bstr_t(value));
Log((LPCTSTR)(_bstr_t("文本信息:")+_bstr_t(value)));
//AfxMessageBox(_bstr_t("文本信息:")+_bstr_t(value)+_bstr_t("\n"),MB_ICONINFORMATION);
}
}
else if(strtype.Find("PASSWORD")!=-1)
{
pInputElement->get_value(&value);
CString str(value);
if(!str.IsEmpty())
{
m_List.AddString(_bstr_t("密码信息:")+_bstr_t(value));
Log((LPCTSTR)(_bstr_t("密码信息:")+_bstr_t(value)));
//AfxMessageBox(_bstr_t("密码信息:")+_bstr_t(value)+_bstr_t("\n"),MB_ICONINFORMATION);
}
}
}
}
}
}
}
}
}
//
_bstr_t bsName=spBrowser->GetLocationName();//窗口名称
//AfxMessageBox(bsName,MB_ICONINFORMATION);
spBrowser->AddRef();
void * pData=spBrowser;
//AfxMessageBox((DWORD)(pData),MB_ICONINFORMATION);
}
}
}
}