Click here to Skip to main content
12,820,283 members (27,853 online)
Click here to Skip to main content
Add your own
alternative version


66 bookmarked
Posted 20 Jul 2003

Web Data Extraction by Crawling using WINHTTP and Document Object (DOM) Instantiation

, 20 Jul 2003
Rate this:
Please Sign up or sign in to vote.
An article on how to extract data from a web URL using the WINHTTP library and how to extract the DOM of the crawled document.


This article deals with two major issues in automatic web data extraction.

  1. How to use WINHTTP 5 library to do crawling (reading Web data).
  2. How to take the data extracted from the WINHTTP and extract/instantiate the DOM out of it.

At the end we discuss how to make a recursive crawler.


The WINHTTP library complies with HTTP 1.0/1.1 model that is based on a persistent (keep alive) protocol model which means that we first connect to a webserver and then make requests for the documents from it. The subsequent requests from a same webserver(hostname in our case) does not involve making and breaking the connection. We discuss here how to extract the HTML data given a string URL to you. The main problem I experienced was that for the crawling you might have a big url given to you. Now this has to be broken up into hostname ( for connection ) and the rest of the url path ( for request ). Of course you would say the WINHTTPCrackUrl method would do this job but it doesn't. It gives you the correct URL Path but not the correct hostname to connect to the server.

For the operating on DOM the most widely used interface series is the IHTMLDocument but an object of this type is usually instantiated and populated by the Browser object ( using the get_document method ). The issue here is how to populate this object with the plain text HTML we get from the WINHTTP.

These two steps go a long way to lay the foundations of a tool which can crawl the web and operate on DOM models of the web pages rather than doing plain string post-processing which most tools do. The similar feat can be obtained by invoking Navigate method on the IE and analyzing the DOM but its trivial to estimate how inefficient it would be to load the whole document (including images) and render it in the browser before getting the DOM.

Why WINHTP when there is WinInet

A traditional developer would say why use WinHTTP when we have the WinInet and it is touted by Microsoft for HTTP( as well as ftp and Gopher ) client applications. But WinInet poses a great stumbling block to total automation. When we do any authentication and some other operations through WinInet it displays a user interface. WinHTTP, however handles these operations programmatically.

How the Program looks

For example if the url to be traversed was then WINHTTP expects to connect to which is the hostname of the webserver and then put in a request for /fc?tmpl=fc&cid=34&in=world&cat=iraq

Given below is a complete description of how to take the URL and spilt it (using WinHttpCrackUrl), doing changes after that because the cracking does not give us the results as we want and then feed this data to the WINHTTP calls After we are done with this; the data extraction from the URL comes into picture and to do this first we connect to the URL and we get the size of the data available on that url using WinHttpQueryDataAvailable. The catch is that we don't get all the data of a web page in one shot so We initialize a buffer to which we'll keep appending the data got from the WinHttpReadData and get the webpage when all the data has been read ( indicated by the available data size being equal to zero). This is exactly how an equivalent URLReader class in java works. Given below is the complete code to do such a feat with explicit comments at each step


    // First, split up the URL
    URL_COMPONENTS urlComp;    // a structure that would contain the
                               // individual components of the URL
    LPCWSTR varURL;            // ***** varURL is the URL to be
                               // traversed
    DWORD dwUrlLen = 0;
    LPCWSTR hostname, optional;

    // Initialize the URL_COMPONENTS structure.
    ZeroMemory(&urlComp, sizeof(urlComp));
    urlComp.dwStructSize = sizeof(urlComp);

    //MessageBox(NULL,OLE2T(varURL),"the url to be traversed", 1);

    // Set required component lengths to non-zero so that they
    // are cracked.
    urlComp.dwSchemeLength    = -1;
    urlComp.dwHostNameLength  = -1;
    urlComp.dwUrlPathLength   = -1;
    urlComp.dwExtraInfoLength = -1;

    // Split the URL (varURL) into hostname and URL path
    if (!WinHttpCrackUrl( varURL, wcslen(pwszUrl1), 0, &urlComp))
        printf("Error %u in WinHttpCrackUrl.\n", GetLastError());
    // You can inspect the cracked URL here
    // For our example of varURL =
    // MessageBox(NULL,W2T(urlComp.lpszHostName),
    //            "INTERPRETER-> hostname",MB_OK);
    // We get the hostname as
    // ""
    //  MessageBox(NULL,W2T(urlComp.lpszUrlPath),
    //             "INTERPRETER-> urlpath",MB_OK);
    // We get the urlPath as "/fc?tmpl=fc&cid=34&in=world&cat=iraq"
    // MessageBox(NULL,W2T(urlComp.lpszExtraInfo),
    //            "INTERPRETER->extrainfo",MB_OK);
    // We get the extrainfo as ""
    // MessageBox(NULL,W2T(urlComp.lpszScheme),
    //            "INTERPRETER->Scheme",MB_OK);
    // We get the scheme as
    // ""
    // Compute the correct hostname
    String myhostname(W2T(urlComp.lpszHostName));
    String myurlpath(W2T(urlComp.lpszUrlPath));
    int strindex = myhostname.IndexOf(myurlpath);
    String newhostname(myhostname.SubString(0,strindex));

    strindex = 0;

    DWORD dwSize        = 0;
    DWORD dwDownloaded  = 0;
    LPSTR pszOutBuffer;
    BOOL  bResults      = FALSE;
    HINTERNET  hSession = NULL,
               hConnect = NULL,
               hRequest = NULL;

    // Use WinHttpOpen to obtain a session handle.
    hSession = WinHttpOpen( L"WinHTTP Example/1.0",
                            WINHTTP_NO_PROXY_BYPASS, 0);

    // Specify an HTTP server.
    // In our examples, it expects just ""
    if (hSession)
        hConnect = WinHttpConnect( hSession, T2W(newhostname),
                                   INTERNET_DEFAULT_HTTP_PORT, 0);

    // Create an HTTP request handle.
    // In our example, it expects
    // "/fc?tmpl=fc&cid=34&in=world&cat=iraq"
    if (hConnect)
        hRequest = WinHttpOpenRequest( hConnect, L"GET",
                                       NULL, WINHTTP_NO_REFERER,
    // Send a request.
    if (hRequest)
        bResults = WinHttpSendRequest( hRequest,
                                              HEADERS, 0,
                                       WINHTTP_NO_REQUEST_DATA, 0,
                                              0, 0);

    // End the request.
    if (bResults)
        bResults = WinHttpReceiveResponse( hRequest, NULL);
        String respage="";    // The buffer that'll contain the
                              // extracted Web page data

    // Keep checking for data until there is nothing left.
    if (bResults)

            // Check for available data.
            dwSize = 0;
            if (!WinHttpQueryDataAvailable( hRequest, &dwSize))
                printf("Error %u in WinHttpQueryDataAvailable.\n",

            // Allocate space for the buffer.
            pszOutBuffer = new char[dwSize+1];
            if (!pszOutBuffer)
                printf("Out of memory\n");
                // Read the Data.
                ZeroMemory(pszOutBuffer, dwSize+1);

                if (!WinHttpReadData( hRequest,
                                      dwSize, &dwDownloaded))
                    printf("Error %u in WinHttpReadData.\n",

                // Free the memory allocated to the buffer.
                delete [] pszOutBuffer;

        } while (dwSize>0);
        // MessageBox(NULL,respage,"fetched page from
        // crawler",1);

When we are done with this, we have the HTML page as a string in the respage buffer. So now the aim is to get a DOM model of this, so that we can operate on the data programmatically like query the nodes, access particular elements and so on. The best way to do DOM manipulation is through the Microsoft provided interfaces IHTMLDocument, IHTMLDocument2, IHTMLDocument3 and IHTMLDocument4. The following code takes data from that buffer and makes an IHTMLDocument2 out of it. We can then use its various methods ( getBody, getInnerHTML, etc. ) to access the DOM or type cast it into a related interface like IHTMLDocument3 and query the nodes in the DOM tree.

// Declare an IHTMLDocument structure
IHTMLDocument2Ptr myDocument; // Declared earlier in the code
HRESULT hr = CoCreateInstance(CLSID_HTMLDocument,NULL,
  CLSCTX_INPROC_SERVER,IID_IHTMLDocument2, (void **)&myDocument);
HRESULT hresult = S_OK;
VARIANT *param;
SAFEARRAY *tmpArray;

// Creates a new one-dimensional array
// for holding the webpage data
tmpArray = SafeArrayCreateVector(VT_VARIANT, 0, 1);
// Convert the buffer into binary string
bstr_t bsData = (LPCTSTR) respage;
hresult = SafeArrayAccessData(sfArray,(LPVOID*) & param);
param->vt = VT_BSTR;
param->bstrVal = bsData;
hresult = myDocument->write(tmpArray);
       // injected code in document structure
hresult = SafeArrayUnaccessData(tmpArray);
if (tmpArray != NULL) {

Further Enhancements

I have highlighted the basics of crawling here and the complete crawler design is a user discretion. For a complete crawler we need to extract the links from a particular web page and extract data from those links. Traditional tools do string processing to look for anchor or the href tags and extract the hyperlink strings and it obviously seems an inefficient way because we have to parse all the page data. Querying the DOM for that purpose is much more efficient; we can just look for all the nodes with an anchor tag and extract the href attribute. Making a web-site grabber is very easy with the code i have given above. You can use the get_anchors method of IHTMLDocument2 to get the the hyperlinks from a page and then recursively call the code above after implementing proper checks for link loops. Such a program can crawl all the hyperlink accessible pages from a given base URL up to any number of levels.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Prabhdeep Singh
Web Developer
United States United States
Prabhdeep (PD) has been a Windows developer for the past 4 years now. He started with commercial development in visual basic and moved onto ATL/COM and C++ subsequently. Has programmed in virtually every language from FORTRAN and COBOL to C# and Java. He did his B.Engg. from India and MS from USA. Currently he is a consultant and technical architect for some start-ups and loves designing novel and killer apps. Still waiting for his ultimate kill which he hopes would come soon. He likes inventing new ideas and thinking innovatively because he says "I guess that is what, being a human is all about". He is crazy about cars, physics and gizmos.

You may also be interested in...


Comments and Discussions

Questionhow to extract web link without navigate? Pin
ravendra0037-Sep-14 20:12
memberravendra0037-Sep-14 20:12 
GeneralWinInet C++ and Java Servlet/Java using SSO Pin
Murali Ch19-Nov-09 2:08
memberMurali Ch19-Nov-09 2:08 
GeneralAbout website extractor Pin
vanijampani18-Sep-09 19:39
membervanijampani18-Sep-09 19:39 
QuestionWhere can I get a list of Daomain names for entire internet Pin
DearSatya25-Feb-08 1:11
memberDearSatya25-Feb-08 1:11 
QuestionPrabhdeep are you interested? Search Engine Expert: Building Web 3.0 Social Networking People Search Engine, Do You Want To Be The King Of The Internet? Pin
rapiddata30-Dec-06 5:24
memberrapiddata30-Dec-06 5:24 
QuestionHow can i Extract data from HTML table Pin
dr_hema30-Dec-05 8:21
memberdr_hema30-Dec-05 8:21 
Generalwhere I can get .h and .lib Pin
Vladimir D27-Nov-05 13:59
memberVladimir D27-Nov-05 13:59 
GeneralRe: where I can get .h and .lib Pin
Ravi Bhavnani27-Nov-05 15:57
memberRavi Bhavnani27-Nov-05 15:57 
GeneralRe: where I can get .h and .lib Pin
Vladimir D27-Nov-05 22:18
memberVladimir D27-Nov-05 22:18 
GeneralRe: where I can get .h and .lib Pin
Vladimir D29-Nov-05 1:32
memberVladimir D29-Nov-05 1:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170308.1 | Last Updated 21 Jul 2003
Article Copyright 2003 by Prabhdeep Singh
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid