vb抓取网页内容(网页中的密码输入框和一般不同的代码是我写的)
优采云 发布时间: 2021-09-20 05:13vb抓取网页内容(网页中的密码输入框和一般不同的代码是我写的)
一、导言
网页中的密码输入框与一般的密码输入框不同。它没有把手。但是,通过获取IE的ihtmlinputtextelement接口,您可以获取网页中输入框(包括文本和密码输入框)的内容
VC知识库首页上运行的源代码效果图如下:
二、特定代码
VARIANT id, index;
CComPtr spDispatch;
CComQIPtr pDoc2;
CComPtr pElement;
CComPtr pElementCol;
CComPtr pFormElement;
CComPtr pInputElement;
//首先获取IWebBrowser2接口
CoInitialize(NULL); //必须要这句初始化
SHDocVw::IWebBrowser2Ptr spBrowser(spDisp);
if (m_spSHWinds == NULL) {
if (m_spSHWinds.CreateInstance(__uuidof(SHDocVw::ShellWindows)) != S_OK) {
MessageBox("Failed");
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) {//获取IHTMLDocument2接口
if (SUCCEEDED(spBrowser->get_Document( &spDispatch)))
pDoc2 = spDispatch;
if(pDoc2!=NULL) {
// AfxMessageBox("已经获取IHTMLDocument2");
if (SUCCEEDED(pDoc2->get_forms(&pElementCol))) {
// AfxMessageBox("已经获取IHTMLElementCollection");
long p=0;
if(SUCCEEDED(pElementCol->get_length(&p)))
if(p!=0) {
for(long i=0;iitem(id,index, &spDispatch)))
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLFormElement,(void**)&pFormElement))) {
// AfxMessageBox("已经获取IHTMLFormElement");
long q=0;
if(SUCCEEDED(pFormElement->get_length(&q)))
for(long j=0;jitem(id,index, &spDispatch)))
if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLInputTextElement,(void**)&pInputElement))) {
//AfxMessageBox("已经获取IHTMLInputTextElement");
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_ctrlIE.InsertItem(0, _bstr_t(value)+_bstr_t(" 【可能是用户名或其他需提交的内容】"));
}
else if(strtype.Find("PASSWORD")!=-1) //获取密码框的值
{
pInputElement->get_value(&value);
CString str(value);
if(!str.IsEmpty())
m_ctrlIE.InsertItem(0, _bstr_t(value) + _bstr_t(" 【应该是密码】"));
}
}
}
}
}
}
}
}
}
}
}
注意:因为我懒惰,本文的框架是一篇题为“如何控制ie行为”的文章。。。感谢这里的原作者,但本文的主要代码是我写的。(事实上,自己编写一个框架太简单了,但我必须去工作:(请原谅!)最好不要向作者寻求技术支持!谢谢你的阅读