VC/MFC 从WebBrower 中获取 HTML 和文本

本文介绍了一种通过获取IE浏览器窗口句柄,并进一步获取IHTMLDocument2接口的方法,从而能够读取和操作浏览器中的HTML元素。该方法适用于需要与IE浏览器进行交互的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文部分转载于 http://blog.chinaunix.net/uid-2516614-id-2496197.html

用于参考

 

///

外部窗口接口获取(非原博客,是自己添加的其他通过获取IE浏览器窗口的句柄,在由句柄转换成IE的接口):

 

 

void CHTMLContrlDlg::OnBnClickedButtonOpen()
{
	// TODO: 在此添加控件通知处理程序代码
	

	HWND ExplorerWnd = ::FindWindow(NULL, _T("CrystalDiskMark 3.0 x64"));

	
	//根据IE主窗口获取浏览器窗口
	  
	  if (!ExplorerWnd)
		::MessageBox(m_hWnd, TEXT("CrystalDiskMark - Internet Explorer"), NULL, MB_OK);

	else
	{
		if (m_WinThread)
		{
			return;
		}

		m_WinThread  = AfxBeginThread(MyThreadRead, (void*)this);
		::SetForegroundWindow(ExplorerWnd);
		FindWindowsHwnd(ExplorerWnd);
	}

}

 

 

 

 

 

 

void CHTMLContrlDlg::FindWindowsHwnd(HWND hWnd)
{

	HWND hWndChild = NULL;
	::EnumChildWindows(hWnd, EnumChildProc, (LPARAM)&hWndChild);

	if (NULL == hWndChild) return;

	UINT nMsg = ::RegisterWindowMessage(_T("WM_HTML_GETOBJECT"));
	LRESULT lRes;

	::SendMessageTimeout(hWndChild, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes);

	CComPtr < IHTMLDocument2 > spDoc;
	
	HRESULT hr = ::ObjectFromLresult(lRes, IID_IHTMLDocument2, 0, (LPVOID *)&spDoc);
	if (FAILED(hr)) return ;
								// 程序运行到此,已经找到了 IHTMLDocument2 的接口指针
	CComBSTR bstrTitle;
	spDoc->get_title(&bstrTitle);//取得文档标题  

	CString str(bstrTitle);

	SetWindowText(str);
	CComPtr < IHTMLElementCollection > spElementCollection;
	hr = spDoc->get_all(&spElementCollection);

	if (FAILED(hr))
	{
		return;
	}
	long elementLength;

	hr = spElementCollection->get_length(&elementLength);获取HTML元素数量
	if (FAILED(hr))
	{
		return;
	}
       //接下来就可以通过指针循环查找元素,根据自己的查找条件判别要找的元素
	VARIANT name;
	CComBSTR tag;
	name.vt = VT_I4;

	for (int i = 0; i < elementLength; i++)
	{
		name.lVal = i;
		IDispatch * pDispatch = NULL;
		HRESULT res = spElementCollection->item(name, name, &pDispatch);

		if (FAILED(res))
		{
			continue;
		}

		CComPtr<IHTMLElement> pHtmlElement;////IHTMLSelectElement是你想找的元素类型,HRESULT hr = ::ObjectFromLresult(lRes, IID_IHTMLDocument2, 0, (LPVOID *)&spDoc);
	if (FAILED(hr)) return ;
								// 程序运行到此,已经找到了 IHTMLDocument2 的接口指针
	CComBSTR bstrTitle;
	spDoc->get_title(&bstrTitle);//取得文档标题  

	CString str(bstrTitle);

	SetWindowText(str);
	CComPtr < IHTMLElementCollection > spElementCollection;
	hr = spDoc->get_all(&spElementCollection);

	if (FAILED(hr))
	{
		return;
	}
	long elementLength;

	hr = spElementCollection->get_length(&elementLength);获取HTML元素数量
	if (FAILED(hr))
	{
		return;
	}
       //接下来就可以通过指针循环查找元素,根据自己的查找条件判别要找的元素
	VARIANT name;
	CComBSTR tag;
	name.vt = VT_I4;

	for (int i = 0; i < elementLength; i++)
	{
		name.lVal = i;
		IDispatch * pDispatch = NULL;
		HRESULT res = spElementCollection->item(name, name, &pDispatch);

		if (FAILED(res))
		{
			continue;
		}

		CComPtr<IHTMLElement> pHtmlElement;////IHTMLSelectElement是你想找的元素类型,
						还有IHTMLSpanElement、IHTMLElement、IHTMLSpanElement等
		hr = pDispatch->QueryInterface(IID_IHTMLElement, (void**)&pHtmlElement);
	        
		hr = pDispatch->QueryInterface(IID_IHTMLElement, (void**)&pHtmlElement);
	        
if (FAILED(hr)) { continue; } BSTR id; BSTR _innerText; pHtmlElement->get_id(&id); pHtmlElement->get_innerText(&_innerText);
           }

}

 

 

 

 

///

 

 

 本代码是关于 从 ACTIVEX控件 WebBrower 中获取 HTML 和文本。
本代码来自网络,从哪里来不记得了。
  原来代码中有获得HTML,但是我项目中的目的是得到HTML中的文本。料想 自己还要写一个HTML-->TXT
的代码来解决最后的问题。写着写着看到 hr=pElement->get_outerHTML();这个代码 和javascript中的inner***很类似。如是利用自动不起功能居然发现了pElement->get_outerText(&pContent);于是直接省略了
HTML-->TXT的代码。。噢 吔!!!

pBrowse = (CWebBrowser2*)this->GetDlgItem(IDC_EXPLORER3);
    IHTMLDocument2 *pHTMLDocument=NULL;
    if (!(pHTMLDocument = (IHTMLDocument2*)pBrowse->GetDocument()))
        return;
    CComPtr<IHTMLElementCollection> pAllColl;
    HRESULT hr;
    hr=pHTMLDocument->get_all(&pAllColl);
    if(hr==S_OK){
        LONG length=0;
        hr=pAllColl->get_length(&length);
        if(hr==S_OK){
            for(int i=0;i<length;i++){
                VARIANT vIndex,vName;
                vName.vt=vIndex.vt=VT_I4;
                vName.lVal=vIndex.lVal=i;
                CComPtr<IDispatch> pDisp;
                hr=pAllColl->item(vName,vIndex,&pDisp);
                if( hr==S_OK ){
                    CComPtr<IHTMLElement> pElement;
                    hr=pDisp->QueryInterface(IID_IHTMLElement,(void**)&pElement);
                    if( hr==S_OK ){
                        CComBSTR tagName;
                        hr=pElement->get_tagName(&tagName);
                        if(hr==S_OK){
                            CString str(tagName);
                            if(str=="HTML"){
                                CComBSTR pContent;
                                hr=pElement->get_outerText(&pContent);
                                //hr=pElement->get_outerHTML();

                                if(hr==S_OK){

                                    UpdateData(true);
                                    m_text = CString(pContent);
                                    UpdateData(false);
                                    i=length;//以便退出循环

                                }
                                else{//if get_outerHTML failed

                                 MessageBox("can't get html code");
                                }
                            }//else if tagName isnot 'HTML'

                        }//else if get_tagName failed

                    }//else if don't get IHMTLElement interface

                }//if no items

            }
        }//if get_length failed

    }//if get_all failed

    pHTMLDocument->Release();
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值