Tcl Requires Chilkat v9.5.0.62+
Tcl
A Simple Web Crawler
See more Spider Examples
This demonstrates a very simple web crawler using the Chilkat Spider component.Chilkat Tcl Downloads
load ./chilkat.dll
set success 0
set spider [new_CkSpider]
# Domain membership and a bounded, last-in-first-out stack of seed URLs.
# Stack keys are consecutive integer positions represented as strings.
set seenDomains [new_CkHashtable]
set seedUrls [new_CkHashtable]
set pendingUrls [new_CkHashtable]
set stackKey [new_CkStringBuilder]
# You will need to change the start URL to something else...
CkHashtable_AddStr $seedUrls "0" "http://something.whateverYouWant.com/"
CkHashtable_AddInt $pendingUrls "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.
CkSpider_AddAvoidOutboundLinkPattern $spider "*?id=*"
CkSpider_AddAvoidOutboundLinkPattern $spider "*.mypages.*"
CkSpider_AddAvoidOutboundLinkPattern $spider "*.personal.*"
CkSpider_AddAvoidOutboundLinkPattern $spider "*.comcast.*"
CkSpider_AddAvoidOutboundLinkPattern $spider "*.aol.*"
CkSpider_AddAvoidOutboundLinkPattern $spider "*~*"
# Use a cache so we don't have to re-fetch URLs previously fetched.
CkSpider_put_CacheDir $spider "c:/spiderCache/"
CkSpider_put_FetchFromCache $spider 1
CkSpider_put_UpdateCache $spider 1
while {[CkHashtable_get_Count $seedUrls] > 0} {
# Remove the last pending URL from both the stack and its membership set.
CkStringBuilder_SetString $stackKey ""
CkStringBuilder_AppendInt $stackKey [expr [CkHashtable_get_Count $seedUrls] - 1]
set url [CkHashtable_lookupStr $seedUrls [CkStringBuilder_getAsString $stackKey]]
CkHashtable_Remove $seedUrls [CkStringBuilder_getAsString $stackKey]
CkHashtable_Remove $pendingUrls $url
CkSpider_Initialize $spider $url
# Spider 5 URLs of this domain.
# but first, save the base domain in seenDomains
set domain [CkSpider_getUrlDomain $spider $url]
CkHashtable_AddInt $seenDomains [CkSpider_getBaseDomain $spider $domain] 1
set numCrawled 0
set success 1
while {expr [$success == 1] && [$numCrawled < 5]} {
set success [CkSpider_CrawlNext $spider]
if {$success == 1} then {
# Display the URL we just crawled.
puts [CkSpider_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 {[CkSpider_get_LastFromCache $spider] != 1} then {
CkSpider_SleepMs $spider 1000
}
set numCrawled [expr $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 {set i 0} {$i <= [expr [CkSpider_get_NumOutboundLinks $spider] - 1]} {incr i} {
set url [CkSpider_getOutboundLink $spider $i]
set domain [CkSpider_getUrlDomain $spider $url]
set baseDomain [CkSpider_getBaseDomain $spider $domain]
if {[CkHashtable_Contains $seenDomains $baseDomain] == 0} then {
# Don't let our list of seedUrls grow too large.
if {expr [[CkHashtable_get_Count $seedUrls] < 1000] && [[CkHashtable_Contains $pendingUrls $url] == 0]} then {
CkStringBuilder_SetString $stackKey ""
CkStringBuilder_AppendInt $stackKey [CkHashtable_get_Count $seedUrls]
CkHashtable_AddStr $seedUrls [CkStringBuilder_getAsString $stackKey] $url
CkHashtable_AddInt $pendingUrls $url 1
}
}
}
}
delete_CkSpider $spider
delete_CkHashtable $seenDomains
delete_CkHashtable $seedUrls
delete_CkHashtable $pendingUrls
delete_CkStringBuilder $stackKey