vc采集网页内所有元素(不指定具体table/form/frame)
1.獨立代碼
//-----------開始---------------------//
#include <atlbase.h>
#include <mshtml.h>
#include <winuser.h>
#include <comdef.h>
#include <string.h>
void EnumIE(void);//處理網頁
CComModule _Module;??//使用CComDispatchDriver ATL的智能指針,此處必須聲明
#include <atlcom.h>
void EnumAllElement(IHTMLDocument2 * pIHTMLDocument2);
?
void EnumIE(void)??
{??
? CComPtr<IShellWindows> spShellWin;??
? HRESULT hr=spShellWin.CoCreateInstance(CLSID_ShellWindows);??
? if (FAILED(hr))??
? {??
? return;??
? }??????
? long nCount=0;??? //取得瀏覽器實例個數(Explorer和IExplorer)??
? spShellWin->get_Count(&nCount);??
? if (0==nCount)??
? {??
??? return;??
? }
? for(int i=0; i<nCount; i++)??
? {??
??? CComPtr<IDispatch> spDispIE;??
??? hr=spShellWin->Item(CComVariant((long)i), &spDispIE);??
??? if (FAILED(hr)) continue;
?
??? CComQIPtr<IWebBrowser2>spBrowser=spDispIE;??
??? if (!spBrowser) continue;
?
??? CComPtr<IDispatch> spDispDoc;??
??? hr=spBrowser->get_Document(&spDispDoc);??
??? if (FAILED(hr)) continue;
?
??? CComQIPtr<IHTMLDocument2>spDocument2 =spDispDoc;??
??? if (!spDocument2) continue;??????
?//Modify by jncao 2007-09-17
?//*******************************************************************************
?CString cIEUrl_Filter;? //設置的URL(必須是此URL的網站才有效);
??? cIEUrl_Filter="http://127.0.0.1/iAgent"; //設置過濾的網址
??? //*******************************************************************************
??? CComBSTR IEUrl;
?spBrowser->get_LocationURL(&IEUrl);
?CString cIEUrl_Get;???? //從機器上取得的HTTP的完整的URL;
?cIEUrl_Get=IEUrl;
?cIEUrl_Get=cIEUrl_Get.Left(cIEUrl_Filter.GetLength()); //截取前面N位
?if (strcmp(cIEUrl_Get,cIEUrl_Filter)==0)
?{
??? ?// 程序運行到此,已經找到了IHTMLDocument2的接口指針???????
???????EnumAllElement(spDocument2);//枚舉所有元素
?}???
? }??
}
void EnumAllElement(IHTMLDocument2 * pIHTMLDocument2) //枚舉所有元素
{
?if (!pIHTMLDocument2) return;??????
?HRESULT?? hr;??
?CComQIPtr<IHTMLElementCollection> spAllElement;
?hr=pIHTMLDocument2->get_all(&spAllElement);//獲取所有網頁內所有元素
?if (FAILED(hr))??return;??
?long nLength = 0;
?spAllElement->get_length (&nLength);
?for (int i = 0; i < nLength; i++)
?{
??????? CComPtr<IDispatch> pDisp;
??hr = spAllElement->item(COleVariant((long)i),COleVariant((long)0),&pDisp); //獲取單個元素
??if(SUCCEEDED(hr))
??{
???//CComQIPtr <IHTMLElement, &IID_IHTMLElement> pElement(pDisp);
???CComQIPtr<IHTMLElement, &IID_IHTMLElement> pElement;
???pDisp->QueryInterface(&pElement);
???BSTR bTemp;
???pElement->get_id(&bTemp);//可以獲取其他特征,根據具體元素而定
???CString strTemp=bTemp;
???if(!strTemp.IsEmpty() && strTemp=="nodule_callerNo_txt")
//根據id是nodule_callerNo_txt(現網主叫text控件ID)獲取值或作其他處理
???{
????IHTMLInputTextElement* input;
????pDisp->QueryInterface(IID_IHTMLInputTextElement,(void**)&input);
????input->get_value(&bTemp);
????strTemp=bTemp;
????AfxMessageBox(strTemp);
???}
??}
?}
}
?
//-----------開始---------------------//
?
2.執行代碼:
void CDemoDlg::OnOK()
{
?// TODO: Add extra validation here
?::CoInitialize(NULL); //初始化COM
???? EnumIE();???????????? //枚舉瀏覽器??????
???? ::CoUninitialize();?? //釋放COM
?//CDialog::OnOK();
}
總結
以上是生活随笔為你收集整理的vc采集网页内所有元素(不指定具体table/form/frame)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: VC采集网页所有表单域
- 下一篇: vc获取网页内链接