Comments (2)
Please take questions like these to the forum
Ask a question, please search for similar questions first!!!
If you are interested in expert Abot customization go here?
from abot.
Figured it out by looking at the Scheduler
class.
To help anyone who wants to do the same, see walkthrough below, noting that this isn't the most elegant code, but it works!.
I hardcoded my list of pages. You would otherwise have some dynamic way of populating your page list(like a DB or even parsing the paging info from the landing/first page)
- Create a custom
IScheduler
where you populate your pages. Custom code is commented. The rest is as-is in theScheduler
class
public class MyScheduler : IScheduler
{
ICrawledUrlRepository _crawledUrlRepo;
IPagesToCrawlRepository _pagesToCrawlRepo;
bool _allowUriRecrawling;
// flag to indicate my list of pages have been loaded
bool _pageListLoaded;
public MyScheduler()
: this(false, null, null)
{
}
public MyScheduler(bool allowUriRecrawling, ICrawledUrlRepository crawledUrlRepo, IPagesToCrawlRepository pagesToCrawlRepo)
{
_allowUriRecrawling = allowUriRecrawling;
_crawledUrlRepo = crawledUrlRepo ?? new CompactCrawledUrlRepository();
_pagesToCrawlRepo = pagesToCrawlRepo ?? new FifoPagesToCrawlRepository();
// custom code different from default Scheduler
// this is where you populate your page list by creating a List of PageToCrawl
var pagesToCrawl = new List<PageToCrawl>();
var listingUrl = "https://www.mysite.com/puppies";
for (var i = 1; i <= 5; i++)
{
pagesToCrawl.Add(new PageToCrawl(new Uri(listingUrl + "?" + i )));
}
// add your list
Add(pagesToCrawl);
// set flag
_pageListLoaded = true;
}
public int Count => _pagesToCrawlRepo.Count();
public void Add(PageToCrawl page)
{
// if your pageList has been loaded, don't allow adding another page
if(_pageListLoaded) return;
if (page == null)
throw new ArgumentNullException("page");
if (_allowUriRecrawling || page.IsRetry)
{
_pagesToCrawlRepo.Add(page);
}
else
{
if (_crawledUrlRepo.AddIfNew(page.Uri))
_pagesToCrawlRepo.Add(page);
}
}
public void Add(IEnumerable<PageToCrawl> pages)
{
// same as above
if (_pageListLoaded) return;
if (pages == null)
throw new ArgumentNullException("pages");
foreach (var page in pages)
Add(page);
}
public void AddKnownUri(Uri uri)
{
_crawledUrlRepo.AddIfNew(uri);
}
public void Clear()
{
_pagesToCrawlRepo.Clear();
}
public void Dispose()
{
if (_crawledUrlRepo != null)
{
_crawledUrlRepo.Dispose();
}
if (_pagesToCrawlRepo != null)
{
_pagesToCrawlRepo.Dispose();
}
}
public PageToCrawl GetNext()
{
return _pagesToCrawlRepo.GetNext();
}
public bool IsUriKnown(Uri uri)
{
return _crawledUrlRepo.Contains(uri);
}
}
- Then, when crawling, based on the QuickStart example, taking note that I used the custom
MyScheduler
to instantiatePoliteWebCrawler
var config = new CrawlConfiguration
{
MaxPagesToCrawl = 10, //Only crawl 10 pages
MinCrawlDelayPerDomainMilliSeconds = 3000, //Wait this many millisecs between requests
};
var scheduler = new MyScheduler();
var crawler = new PoliteWebCrawler(config,null,null,scheduler,null,null,null,null,null);
crawler.PageCrawlCompleted += PageCrawlCompleted;//Several events available...
var crawlResult = await crawler.CrawlAsync(new Uri("https://www.mysite.com/"));
- That's it! Have fun!
from abot.
Related Issues (20)
- How to implement the decision maker function HOT 1
- User Agent config value appears to be getting split on spaces and sending requests with multiple user-agent headers HOT 2
- Update Readme file? HOT 1
- Collection of links found HOT 1
- Sitemap.xml parsedlinks is empty HOT 5
- Bot stop crawling without any error
- Handling pagination HOT 1
- Best way to do page-by-page crawling HOT 2
- Incorrect usage of ThreadManager means it does nothing HOT 4
- The PageRequester downloads the content by default HOT 1
- Exception is thrown when redirect URI is relative HOT 1
- Base URI logic of the HyperLinkParser breaks on Linux when faced with a root-relative base tag value HOT 2
- Base URI logic of the HyperLinkParser doesn't respect terminated relative base tag values HOT 2
- Question HOT 1
- Control maximum number of concurrent pages to be crawled HOT 1
- Are 304 responses properly handled? HOT 2
- Crawl .jsp sites
- Content.Text empty despite response code OK and Content stream contains data
- Cancellation is not working properly and integration test is wrong HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from abot.