PowerBuilder Requires Chilkat v9.5.0.62+
PowerBuilder
A Simple Web Crawler
See more Spider Examples
This demonstrates a very simple web crawler using the Chilkat Spider component.Chilkat PowerBuilder Downloads
integer li_rc
long li_Success
oleobject loo_Spider
oleobject loo_SeenDomains
oleobject loo_SeedUrls
oleobject loo_PendingUrls
oleobject loo_StackKey
string ls_Url
string ls_Domain
long i
long li_NumCrawled
string ls_BaseDomain
li_Success = 0
loo_Spider = create oleobject
li_rc = loo_Spider.ConnectToNewObject("Chilkat.Spider")
if li_rc < 0 then
destroy loo_Spider
MessageBox("Error","Connecting to COM object failed")
return
end if
// Domain membership and a bounded, last-in-first-out stack of seed URLs.
// Stack keys are consecutive integer positions represented as strings.
loo_SeenDomains = create oleobject
li_rc = loo_SeenDomains.ConnectToNewObject("Chilkat.Hashtable")
loo_SeedUrls = create oleobject
li_rc = loo_SeedUrls.ConnectToNewObject("Chilkat.Hashtable")
loo_PendingUrls = create oleobject
li_rc = loo_PendingUrls.ConnectToNewObject("Chilkat.Hashtable")
loo_StackKey = create oleobject
li_rc = loo_StackKey.ConnectToNewObject("Chilkat.StringBuilder")
// You will need to change the start URL to something else...
loo_SeedUrls.AddStr("0","http://something.whateverYouWant.com/")
loo_PendingUrls.AddInt("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.
loo_Spider.AddAvoidOutboundLinkPattern("*?id=*")
loo_Spider.AddAvoidOutboundLinkPattern("*.mypages.*")
loo_Spider.AddAvoidOutboundLinkPattern("*.personal.*")
loo_Spider.AddAvoidOutboundLinkPattern("*.comcast.*")
loo_Spider.AddAvoidOutboundLinkPattern("*.aol.*")
loo_Spider.AddAvoidOutboundLinkPattern("*~~*")
// Use a cache so we don't have to re-fetch URLs previously fetched.
loo_Spider.CacheDir = "c:/spiderCache/"
loo_Spider.FetchFromCache = 1
loo_Spider.UpdateCache = 1
do while loo_SeedUrls.Count > 0
// Remove the last pending URL from both the stack and its membership set.
loo_StackKey.SetString("")
loo_StackKey.AppendInt(loo_SeedUrls.Count - 1)
ls_Url = loo_SeedUrls.LookupStr(loo_StackKey.GetAsString())
loo_SeedUrls.Remove(loo_StackKey.GetAsString())
loo_PendingUrls.Remove(ls_Url)
loo_Spider.Initialize(ls_Url)
// Spider 5 URLs of this domain.
// but first, save the base domain in seenDomains
ls_Domain = loo_Spider.GetUrlDomain(ls_Url)
loo_SeenDomains.AddInt(loo_Spider.GetBaseDomain(ls_Domain),1)
li_NumCrawled = 0
li_Success = 1
do while (li_Success = 1) AND (li_NumCrawled < 5)
li_Success = loo_Spider.CrawlNext()
if li_Success = 1 then
// Display the URL we just crawled.
Write-Debug loo_Spider.LastUrl
// If the last URL was retrieved from cache,
// we won't wait. Otherwise we'll wait 1 second
// before fetching the next URL.
if loo_Spider.LastFromCache <> 1 then
loo_Spider.SleepMs(1000)
end if
li_NumCrawled = li_NumCrawled + 1
end if
// If CrawlNext fails (no more URLs to crawl in this domain), success is false and the loop exits.
loop
// Add the outbound links to seedUrls, except
// for the domains we've already seen.
for i = 0 to loo_Spider.NumOutboundLinks - 1
ls_Url = loo_Spider.GetOutboundLink(i)
ls_Domain = loo_Spider.GetUrlDomain(ls_Url)
ls_BaseDomain = loo_Spider.GetBaseDomain(ls_Domain)
if loo_SeenDomains.Contains(ls_BaseDomain) = 0 then
// Don't let our list of seedUrls grow too large.
if (loo_SeedUrls.Count < 1000) AND (loo_PendingUrls.Contains(ls_Url) = 0) then
loo_StackKey.SetString("")
loo_StackKey.AppendInt(loo_SeedUrls.Count)
loo_SeedUrls.AddStr(loo_StackKey.GetAsString(),ls_Url)
loo_PendingUrls.AddInt(ls_Url,1)
end if
end if
next
loop
destroy loo_Spider
destroy loo_SeenDomains
destroy loo_SeedUrls
destroy loo_PendingUrls
destroy loo_StackKey