Sample code for 30+ languages & platforms
Lua 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 Lua Downloads

Lua
local chilkat = require("chilkat")

local success = false

local spider = chilkat.Spider.new()

-- Domain membership and a bounded, last-in-first-out stack of seed URLs.
-- Stack keys are consecutive integer positions represented as strings.
local seenDomains = chilkat.Hashtable.new()
local seedUrls = chilkat.Hashtable.new()
local pendingUrls = chilkat.Hashtable.new()
local stackKey = chilkat.StringBuilder.new()

-- You will need to change the start URL to something else...
seedUrls:add_str("0", "http://something.whateverYouWant.com/")
pendingUrls:add_int("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.
spider:add_avoid_outbound_link_pattern("*?id=*")
spider:add_avoid_outbound_link_pattern("*.mypages.*")
spider:add_avoid_outbound_link_pattern("*.personal.*")
spider:add_avoid_outbound_link_pattern("*.comcast.*")
spider:add_avoid_outbound_link_pattern("*.aol.*")
spider:add_avoid_outbound_link_pattern("*~*")

-- Use a cache so we don't have to re-fetch URLs previously fetched.
spider.cache_dir = "c:/spiderCache/"
spider.fetch_from_cache = true
spider.update_cache = true

while seedUrls.count > 0 do
    -- Remove the last pending URL from both the stack and its membership set.
    stackKey:set_string("")
    stackKey:append_int(seedUrls.count - 1)
    local url = seedUrls:lookup_str(stackKey:get_as_string())
    seedUrls:remove(stackKey:get_as_string())
    pendingUrls:remove(url)
    spider:initialize(url)

    -- Spider 5 URLs of this domain.
    -- but first, save the base domain in seenDomains
    local domain = spider:get_url_domain(url)
    seenDomains:add_int(spider:get_base_domain(domain), 1)

    local numCrawled = 0
    success = true
    while (success) and (numCrawled < 5) do
        success = spider:crawl_next()
        if success then
            -- Display the URL we just crawled.
            print(spider.last_url)

            -- If the last URL was retrieved from cache,
            -- we won't wait.  Otherwise we'll wait 1 second
            -- before fetching the next URL.
            if not spider.last_from_cache then
                spider:sleep_ms(1000)
            end

            numCrawled = numCrawled + 1
        end

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

    -- Add the outbound links to seedUrls, except
    -- for the domains we've already seen.
    for i = 0, spider.num_outbound_links - 1 do
        url = spider:get_outbound_link(i)
        domain = spider:get_url_domain(url)
        local baseDomain = spider:get_base_domain(domain)
        if not seenDomains:contains(baseDomain) then
            -- Don't let our list of seedUrls grow too large.
            if (seedUrls.count < 1000) and (not pendingUrls:contains(url)) then
                stackKey:set_string("")
                stackKey:append_int(seedUrls.count)
                seedUrls:add_str(stackKey:get_as_string(), url)
                pendingUrls:add_int(url, 1)
            end
        end
    end
end