Programming C++Builder How to Get the Html Source Content From TCppWebBrowser in C Builder
Notice: Undefined variable: relatedArticlesCount in /home/wwwroot/codeback.net/plugins/content/tags.php on line 149

How to get the html content from TCppWebBrowser in C Builder?From codeback.net

Here I supply two methods, if you just want to get the html content of body tag, you could use the following codes. If you want to run this program, just create a new project, put a CppWebBrowser, a Memo and 3 buttons onto the form, then assign the codes to the button's handler. You could get all the project source codes here:

Get Html Source in TCppWebBrowser

//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
    if (CppWebBrowser1->Document == NULL)
        return;

    BSTR strContent;
    IHTMLDocument2* pDoc;
    if (SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc)))
    {
        IHTMLElement* pBody;
        pDoc->get_body(&pBody);
        pBody->get_innerHTML(&strContent);
        pDoc->close();
        pDoc->Release();
    }
    Memo1->Text = AnsiString(strContent);
}

While if you want to get all the web document's html source, you have to use the method like this:

//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
    IHTMLDocument2* pDoc;
    IDispatch* spDispatch;
    IHTMLElement* pElement;
    IHTMLElementCollection* pElementCol;

    if (SUCCEEDED(CppWebBrowser1->Document->QueryInterface(IID_IHTMLDocument2, (void**)&pDoc)))
    {
        if (SUCCEEDED(pDoc->get_all(&pElementCol)))
        {
            long lLength = 0;
            if (SUCCEEDED(pElementCol->get_length(&lLength)))
            {
                for (int i = 0; i < lLength; ++i)
                {
                    VARIANT index;
                    index.vt = VT_I4;
                    index.lVal = i;
                    if (SUCCEEDED(pElementCol->item(index, index, &spDispatch)))
                    {
                        if(SUCCEEDED(spDispatch->QueryInterface(IID_IHTMLElement, (void**)&pElement)))
                        {
                            BSTR TagName = NULL;
                            pElement->get_tagName(&TagName);

                            if (AnsiString(TagName).AnsiPos("HTML") > 0)
                            {
                                BSTR HtmlContent;
                                pElement->get_outerHTML(&HtmlContent);
                                Memo1->Text = AnsiString(HtmlContent);
                                SysFreeString(HtmlContent);
                            }
                            SysFreeString(TagName);

                            pElement->Release();
                        }
                        spDispatch->Release();
                    }

                }//end of for

            }
            pElementCol->Release();
        }

        pDoc->Release();
    }
}
//---------------------------------------------------------------------------

If you are using Delphi, see the save method written in pascal codes here: http://codeback.net/how-to-get-a-webpages-html-source-by-using-twebbrowser-component-in-delphi


Add comment


Security code
Refresh

Programming - C++Builder