Sample code for 30+ languages & platforms
Unicode C Requires Chilkat v9.5.0.62+

A Simple Web Crawler

See more Spider Examples

This demonstrates a very simple web crawler using the Chilkat Spider component.

Chilkat Unicode C Downloads

Unicode C
#include <C_CkSpiderW.h>
#include <C_CkHashtableW.h>
#include <C_CkStringBuilderW.h>

void ChilkatSample(void)
    {
    BOOL success;
    HCkSpiderW spider;
    HCkHashtableW seenDomains;
    HCkHashtableW seedUrls;
    HCkHashtableW pendingUrls;
    HCkStringBuilderW stackKey;
    const wchar_t *url;
    const wchar_t *domain;
    int i;
    int numCrawled;
    const wchar_t *baseDomain;

    success = FALSE;

    spider = CkSpiderW_Create();

    //  Domain membership and a bounded, last-in-first-out stack of seed URLs.
    //  Stack keys are consecutive integer positions represented as strings.
    seenDomains = CkHashtableW_Create();
    seedUrls = CkHashtableW_Create();
    pendingUrls = CkHashtableW_Create();
    stackKey = CkStringBuilderW_Create();

    //  You will need to change the start URL to something else...
    CkHashtableW_AddStr(seedUrls,L"0",L"http://something.whateverYouWant.com/");
    CkHashtableW_AddInt(pendingUrls,L"http://something.whateverYouWant.com/",1);

    //  Set outbound URL exclude patterns
    //  URLs matching any of these patterns will not be added to the 
    //  collection of outbound links.
    CkSpiderW_AddAvoidOutboundLinkPattern(spider,L"*?id=*");
    CkSpiderW_AddAvoidOutboundLinkPattern(spider,L"*.mypages.*");
    CkSpiderW_AddAvoidOutboundLinkPattern(spider,L"*.personal.*");
    CkSpiderW_AddAvoidOutboundLinkPattern(spider,L"*.comcast.*");
    CkSpiderW_AddAvoidOutboundLinkPattern(spider,L"*.aol.*");
    CkSpiderW_AddAvoidOutboundLinkPattern(spider,L"*~*");

    //  Use a cache so we don't have to re-fetch URLs previously fetched.
    CkSpiderW_putCacheDir(spider,L"c:/spiderCache/");
    CkSpiderW_putFetchFromCache(spider,TRUE);
    CkSpiderW_putUpdateCache(spider,TRUE);

    while (CkHashtableW_getCount(seedUrls) > 0) {

        //  Remove the last pending URL from both the stack and its membership set.
        CkStringBuilderW_SetString(stackKey,L"");
        CkStringBuilderW_AppendInt(stackKey,CkHashtableW_getCount(seedUrls) - 1);
        url = CkHashtableW_lookupStr(seedUrls,CkStringBuilderW_getAsString(stackKey));
        CkHashtableW_Remove(seedUrls,CkStringBuilderW_getAsString(stackKey));
        CkHashtableW_Remove(pendingUrls,url);
        CkSpiderW_Initialize(spider,url);

        //  Spider 5 URLs of this domain.
        //  but first, save the base domain in seenDomains
        domain = CkSpiderW_getUrlDomain(spider,url);
        CkHashtableW_AddInt(seenDomains,CkSpiderW_getBaseDomain(spider,domain),1);

        numCrawled = 0;
        success = TRUE;
        while ((success == TRUE) && (numCrawled < 5)) {
            success = CkSpiderW_CrawlNext(spider);
            if (success == TRUE) {
                //  Display the URL we just crawled.
                wprintf(L"%s\n",CkSpiderW_lastUrl(spider));

                //  If the last URL was retrieved from cache,
                //  we won't wait.  Otherwise we'll wait 1 second
                //  before fetching the next URL.
                if (CkSpiderW_getLastFromCache(spider) != TRUE) {
                    CkSpiderW_SleepMs(spider,1000);
                }

                numCrawled = numCrawled + 1;
            }

            //  If CrawlNext fails (no more URLs to crawl in this domain), success is false and the loop exits.
        }

        //  Add the outbound links to seedUrls, except
        //  for the domains we've already seen.
        for (i = 0; i <= CkSpiderW_getNumOutboundLinks(spider) - 1; i++) {

            url = CkSpiderW_getOutboundLink(spider,i);
            domain = CkSpiderW_getUrlDomain(spider,url);
            baseDomain = CkSpiderW_getBaseDomain(spider,domain);
            if (CkHashtableW_Contains(seenDomains,baseDomain) == FALSE) {
                //  Don't let our list of seedUrls grow too large.
                if ((CkHashtableW_getCount(seedUrls) < 1000) && (CkHashtableW_Contains(pendingUrls,url) == FALSE)) {
                    CkStringBuilderW_SetString(stackKey,L"");
                    CkStringBuilderW_AppendInt(stackKey,CkHashtableW_getCount(seedUrls));
                    CkHashtableW_AddStr(seedUrls,CkStringBuilderW_getAsString(stackKey),url);
                    CkHashtableW_AddInt(pendingUrls,url,1);
                }

            }

        }

    }



    CkSpiderW_Dispose(spider);
    CkHashtableW_Dispose(seenDomains);
    CkHashtableW_Dispose(seedUrls);
    CkHashtableW_Dispose(pendingUrls);
    CkStringBuilderW_Dispose(stackKey);

    }