Class WebCrawlerConfig
Web Crawler configuration, adding more options to the base
CrawlerConfig.
Start URLs
Crawling begins with specifying one or more references to either documents or starting points to documents you want to crawl. For a web crawl, those references are URLs (e.g., web site home page, sitemap, etc.). There are different ways to specify one or more "start" references (repeatable, shortened to "ref"):
- ref: Any regular URL
(see
CrawlerConfig.setStartReferences(List)). - refsFile: A path to a file containing a list of start URLs
(see
CrawlerConfig.setStartReferencesFiles(List)). One per line. - sitemap: A URL pointing to a sitemap XML file that contains
the URLs to crawl (see
setStartReferencesSitemaps(List)). - provider: Your own class implementing
ReferencesProviderto dynamically provide a list of start URLs (seeCrawlerConfig.setStartReferencesProviders(List)).
Scope: To limit crawling to specific web domains, and avoid creating
many filters to that effect, you can tell the crawler to "stay" within
the web site "scope" with
setUrlScopeResolver(UrlScopeResolver).
URL Normalization
Pages on web sites are often referenced using different URL
patterns. Such URL variations can fool the crawler into downloading the
same document multiple times. To avoid this, URLs are "normalized". That is,
they are converted so they are always formulated the same way.
By default, the crawler only applies normalization in ways that are
semantically equivalent (see GenericUrlNormalizer).
Crawl Speed
Be kind to web sites you crawl. Being too aggressive can be perceived as a cyber-attack by the targeted web site (e.g., DoS attack). This can lead to your crawler being blocked.
For this reason, the crawler plays nice by default. It will wait a
few seconds between each page download, regardless of the maximum
number of threads specified or whether pages crawled are on different
web sites. This can of course be changed to be as fast as you want.
See GenericDelayResolver)
for changing default options. You can also provide your own "delay resolver"
by supplying a class implementing DelayResolver.
Crawl Depth
The crawl depth represents how many level from the start URL the crawler
goes. From a browser user perspective, it can be seen as the number of
link "clicks" required from a start URL in order to get to a specific page.
The crawler will crawl as deep for as long as it discovers new URLs
not getting rejected by your configuration. This is not always desirable.
For instance, a web site could have dynamically generated URLs with infinite
possibilities (e.g., dynamically generated web calendars). To avoid
infinite crawls, it is recommended to limit the maximum depth to something
reasonable for your site with CrawlerConfig.setMaxDepth(int).
Keeping Referenced Links
By default the crawler stores, as metadata, URLs extracted from
documents that are in scope. Exceptions
are pages discovered at the configured maximum depth
(CrawlerConfig.setMaxDepth(int)).
This can be changed using the
setKeepReferencedLinks(Set) method.
Changing this setting has no incidence on what page gets crawled.
Possible options are:
- INSCOPE: Default. Store "in-scope" links as
WebDocMetadata.REFERENCED_URLS. - OUTSCOPE: Store "out-of-scope" links as
WebDocMetadata.REFERENCED_URLS_OUT_OF_SCOPE. - MAXDEPTH: Also store links extracted on pages at max depth. Must be used with at least one other option to have any effect.
Orphan documents
Orphans are valid documents, which on subsequent crawls can no longer be
reached (e.g. there are no longer referenced). This is
regardless whether the file has been deleted or not at the source.
You can tell the crawler how to handle those with
CrawlerConfig.setOrphansStrategy(OrphansStrategy). Possible options are:
- PROCESS: Default. Tries to crawl orphans normally as if they were still reachable by the crawler.
- IGNORE: Does nothing with orphans (not deleted, not processed)..
- DELETE: Orphans are sent to your Committer for deletion.
Error Handling
By default the crawler logs exceptions while trying to prevent them
from terminating a crawling session. There might be cases where you want
the crawler to halt upon encountering some types of exceptions.
You can do so with CrawlerConfig.setStopOnExceptions(List).
Crawler Events
The crawler fires all kind of events to notify interested parties of such
things as when a document is rejected, imported, committed, etc.).
You can listen to crawler events using CrawlerConfig.setEventListeners(List).
Document Importing
The process of transforming, enhancing, parsing to extracting plain text
and many other document-specific processing activities are handled by the
Norconex Importer module. See ImporterConfig for many
additional configuration options.
Bad Documents
On a fresh crawl, documents that are unreachable or not obtained
successfully for some reason are simply logged and ignored.
On the other hand, documents that were successfully crawled once
and are suddenly failing on a subsequent crawl are considered "spoiled".
You can decide whether to grace (retry next time), delete, or ignore
those spoiled documents with
CrawlerConfig.setSpoiledReferenceStrategizer(SpoiledReferenceStrategizer).
Committing Documents
The last step of a successful processing of a document is to
store it in your preferred target repository (or repositories).
For this to happen, you have to configure one or more Committers
corresponding to your needs or create a custom one.
You can have a look at available Committers here:
https://opensource.norconex.com/committers/
See CrawlerConfig.setCommitters(List).
HTTP Fetcher
To crawl and parse a document, it first needs to be downloaded. This is the
role of one or more HTTP Fetchers. HttpClientFetcher is the
default implementation and can handle most web sites.
There might be cases where a more specialized way of obtaining web resources
is needed. For instance, JavaScript-generated web pages are often best
handled by web browsers. In such case you can use the
WebDriverFetcher. You can also use
CrawlerConfig.setFetchers(List) to supply your own fetcher implementation.
HTTP Methods
A fetcher typically issues an HTTP GET request to obtain a document. There might be cases where you first want to issue a separate HEAD request. One example is to filter documents based on the HTTP HEAD response information, thus possibly saving downloading large files you don't want.
You can tell the crawler how it should handle HTTP GET and HEAD requests
using using CrawlerConfig.setDocumentFetchSupport(FetchDirectiveSupport) and
CrawlerConfig.setMetadataFetchSupport(FetchDirectiveSupport) respectively.
For each, the options are:
- DISABLED: No HTTP call will be made using that method.
- OPTIONAL: If the HTTP method is not supported by any fetcher or the HTTP request for it was not successful, the document can still be processed successfully by the other HTTP method. Only relevant when both HEAD and GET are enabled.
- REQUIRED: If the HTTP method is not supported by any fetcher or the HTTP request for it was not successful, the document will be rejected and won't go any further, even if the other HTTP method was or could have been successful. Only relevant when both HEAD and GET are enabled.
If you enable only one HTTP method (default), then specifying OPTIONAL or REQUIRED for it have the same effect. At least one method needs to be enabled for an HTTP request to be attempted. By default HEAD requests are DISABLED and GET are REQUIRED. If you are unsure what settings to use, keep the defaults.
Filtering Unwanted Documents
Without filtering, you would typically crawl many documents you are not interested in. There are different types filtering offered to you, occurring at different time during a URL crawling process. The sooner in a URL processing life-cycle you filter out a document the more you can improve the crawler performance. It may be important for you to understand the differences:
- Reference filters: The fastest way to exclude a document. The filtering rule applies on the URL, before any HTTP request is made for that URL. Rejected documents are not queued for processing. They are not downloaded (thus no URLs are extracted). The specified "delay" between downloads is not applied (i.e. no delay for rejected documents).
-
Metadata filters: Applies filtering on a document metadata fields.
If
CrawlerConfig.getMetadataFetchSupport()value forces a distinct call for fetching metadata, these filters will be invoked after the crawler performs an HTTP HEAD request. It gives you the opportunity to filter documents based on the HTTP HEAD response to potentially save a more expensive HTTP GET request for download (but results in two HTTP requests for valid documents -- HEAD and GET). Filtering occurs before URLs are extracted (since no content is downloaded.When
CrawlerConfig.getMetadataFetchSupport()does not invoke making a distinct call for metadata, these filters will be invoked on the metadata of the HTTP response obtained from an HTTP GET request (as the document is downloaded). Filtering occurs after URLs are extracted. - Document filters: Use when having access to the document itself (and its content) is required to apply filtering. Always triggered after a document is downloaded and after URLs are extracted, but before it is imported (Importer module).
- Importer filters: The Importer module also offers document filtering options. At that point a document is already downloaded and its links extracted. There are two types of filtering offered by the Importer: before and after document parsing (assuming you configured at least one parser). Use filters before parsing if you need to filter on raw content or want to avoid parsing some documents. Use filters after parsing when you need to read the content as plain text.
Robot Directives
By default, the crawler tries to respect instructions a web site has put
in place for the benefit of crawlers. The following is a list of some of the
popular ones. Where null can be set to disable support
for specific instructions, you can achieve the equivalent in XML
configuration by declaring the corresponding option as a self-closed tag.
-
"robots.txt" rules: Rules defined in a "robots.txt" file at the
root of a web site.
Defaults to
StandardRobotsTxtProvider. Set tonullviasetRobotsTxtProvider(RobotsTxtProvider)to disable support for "robots.txt" rules. -
Robots metadata rules: Rules provided via the HTTP response
header
X-Robots-Tagfor a given document. Defaults toStandardRobotsMetaProvider. Set tonullviasetRobotsMetaProvider(RobotsMetaProvider)to disable support for robots metadata rules. -
HTML "nofollow": Most HTML-oriented link extractors support
the
rel="nofollow"attribute set on HTML links and offer a way to disable this instruction. E.g.,HtmlLinkExtractorConfig.setIgnoreNofollow(boolean). -
Sitemap: Sitemaps XML files contain as listing of
website URLs typically worth crawling. They can be detected by
looking at usual website locations or via robots.txt instructions, or
they can be specified via
setStartReferencesSitemaps(List). Defaults toGenericSitemapResolver, which offers support for disabling sitemap detection to rely only on sitemap start references. Setting it tonullviasetSitemapResolver(SitemapResolver)effectively disables sitemap support altogether, and is thus incompatible with sitemaps specified as start references. -
Canonical URLs: The crawler will reject URLs that are
non-canonical, as per HTML
<meta ...>or HTTP response instructions. Defaults toGenericCanonicalLinkDetector. Set tonullviasetCanonicalLinkDetector(CanonicalLinkDetector)to disable support canonical links (increasing the chance of getting duplicates). -
Fetcher-specific: Fetcher implementations may support additional
web site instructions with corresponding configuration options.
For example, the default HTTP Fetcher (
HttpClientFetcher) supports theIf-Modified-Sincefor web sites supporting it (only affects incremental crawls). To turn that off, useHttpClientFetcherConfig.setIfModifiedSinceDisabled(boolean). See fetcher documentation for additional options.
Re-crawl Frequency
The crawler will crawl any given URL at most one time per crawling session.
It is possible to skip documents that are not yet "ready" to be re-crawled
to speed up each crawling sessions.
Sitemap.xml directives to that effect are respected by default
("frequency" and "lastmod"). You can have your own conditions for re-crawl
with setRecrawlableResolver(RecrawlableResolver).
This feature can be used for instance, to crawl a "news" section of your
site more frequently than let's say, an "archive" section of your site.
Change Detection (Checksums)
To find out if a document has changed from one crawling session to another,
the crawler creates and keeps a digital signature, or checksum of each
crawled documents. Upon crawling the same URL again, a new checksum
is created and compared against the previous one. Any difference indicates
a modified document. There are two checksums at play, tested at
different times. One obtained from
a document metadata (default is LastModifiedMetadataChecksummer,
and one from the document itself Md5DocumentChecksummer. You can
provide your own implementation. See:
CrawlerConfig.setMetadataChecksummer(MetadataChecksummer) and
CrawlerConfig.setDocumentChecksummer(DocumentChecksummer).
Deduplication
The crawler can attempt to detect and reject documents considered as
duplicates within a crawler session. A document will be considered
duplicate if there was already a document processed with the same
metadata or document checksum. To enable this feature, set
CrawlerConfig.setMetadataDeduplicate(boolean) and/or
CrawlerConfig.setDocumentDeduplicate(boolean) to true. Setting
those will have no effect if the corresponding checksummers are
null or checksums are otherwise not are being generated.
Deduplication can impact crawl performance. It is recommended you use it only if you can't distinguish duplicates via other means (URL normalizer, canonical URL support, etc.). Also, you should only enable this feature if you know your checksummer(s) will generate a checksum that is acceptably unique to you.
URL Extraction
To be able to crawl a web site, links need to be extracted from
web pages. It is the job of a link extractor. It is possible to use
multiple link extractor for different type of content. By default,
the HtmlLinkExtractor is used, but you can add others or
provide your own with setLinkExtractors(List).
There might be
cases where you want a document to be parsed by the Importer and establish
which links to process yourself during the importing phase (for more
advanced use cases). In such cases, you can identify a document metadata
field to use as a URL holding tanks after importing has occurred.
URLs in that field will become eligible for crawling.
See setPostImportLinks(TextMatcher).
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic final classstatic enumFlags for storing as metadata a page referenced links.Nested classes/interfaces inherited from class com.norconex.crawler.core.CrawlerConfig
CrawlerConfig.ChangeDiscovery, CrawlerConfig.OrphansStrategy -
Field Summary
Fields inherited from class com.norconex.crawler.core.CrawlerConfig
DEFAULT_IDLE_TIMEOUT, DEFAULT_MIN_PROGRESS_LOGGING_INTERVAL, DEFAULT_WORKDIR -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected booleanbooleanThe canonical link detector.The delay resolver dictating the minimum amount of time to wait between web requests.Gets what type of referenced links to keep, if any.Gets link extractors.Gets a field matcher used to identify post-import metadata fields holding URLs to consider for crawling.The resolver that indicates whether a given URL is ready to be crawled by a new crawl session.The provider of robots metadata rules for a page (if applicable).The provider of robots.txt rules for a site (if applicable).The locator of sitemaps (if applicable).The resolver of web site sitemaps (if applicable).Gets sitemap URLs to be used as starting points for crawling.Gets URL normalizers.The strategy to use to determine if a URL is in scope.inthashCode()booleanWhether to keep the Importer-populated fields fromgetPostImportLinks().setCanonicalLinkDetector(CanonicalLinkDetector canonicalLinkDetector) The canonical link detector.setDelayResolver(DelayResolver delayResolver) The delay resolver dictating the minimum amount of time to wait between web requests.setKeepReferencedLinks(Set<WebCrawlerConfig.ReferencedLinkType> keepReferencedLinks) Sets whether to keep referenced links and what to keep.setLinkExtractors(List<LinkExtractor> linkExtractors) Sets link extractors.setPostImportLinks(TextMatcher fieldMatcher) Set a field matcher used to identify post-import metadata fields holding URLs to consider for crawling.setPostImportLinksKeep(boolean postImportLinksKeep) Whether to keep the Importer-populated fields fromgetPostImportLinks().setRecrawlableResolver(RecrawlableResolver recrawlableResolver) The resolver that indicates whether a given URL is ready to be crawled by a new crawl session.setRobotsMetaProvider(RobotsMetaProvider robotsMetaProvider) The provider of robots metadata rules for a page (if applicable).setRobotsTxtProvider(RobotsTxtProvider robotsTxtProvider) The provider of robots.txt rules for a site (if applicable).setSitemapLocator(SitemapLocator sitemapLocator) The locator of sitemaps (if applicable).setSitemapResolver(SitemapResolver sitemapResolver) The resolver of web site sitemaps (if applicable).setStartReferencesSitemaps(List<String> startReferencesSitemaps) Sets the sitemap URLs used as starting points for crawling.setUrlNormalizers(List<WebUrlNormalizer> urlNormalizers) Sets URL normalizers.setUrlScopeResolver(UrlScopeResolver urlScopeResolver) The strategy to use to determine if a URL is in scope.toString()Methods inherited from class com.norconex.crawler.core.CrawlerConfig
addCommitter, addEventListener, addEventListeners, clearEventListeners, getChangeDiscovery, getClusterConfig, getCommitters, getDeferredShutdownDuration, getDocumentChecksummer, getDocumentFetchSupport, getDocumentFilters, getEventListeners, getFetchers, getFetchersMaxRetries, getFetchersRetryDelay, getId, getIdleTimeout, getImporterConfig, getMaxCrawlDuration, getMaxDepth, getMaxDocuments, getMaxQueueBatchSize, getMaxStreamCachePoolSize, getMaxStreamCacheSize, getMetadataChecksummer, getMetadataFetchSupport, getMetadataFilters, getMinProgressLoggingInterval, getNumThreads, getOrphansStrategy, getPostImportConsumers, getPreImportConsumers, getReferenceFilters, getSpoiledReferenceStrategizer, getStartReferences, getStartReferencesFiles, getStartReferencesProviders, getStopOnExceptions, getWorkDir, isDocumentDeduplicate, isMetadataDeduplicate, isStartReferencesAsync, removeEventListener, setChangeDiscovery, setClusterConfig, setCommitters, setDeferredShutdownDuration, setDocumentChecksummer, setDocumentDeduplicate, setDocumentFetchSupport, setDocumentFilters, setEventListeners, setFetchers, setFetchersMaxRetries, setFetchersRetryDelay, setId, setIdleTimeout, setImporterConfig, setMaxCrawlDuration, setMaxDepth, setMaxDocuments, setMaxQueueBatchSize, setMaxStreamCachePoolSize, setMaxStreamCacheSize, setMetadataChecksummer, setMetadataDeduplicate, setMetadataFetchSupport, setMetadataFilters, setMinProgressLoggingInterval, setNumThreads, setOrphansStrategy, setPostImportConsumers, setPreImportConsumers, setReferenceFilters, setSpoiledReferenceStrategizer, setStartReferences, setStartReferencesAsync, setStartReferencesFiles, setStartReferencesProviders, setStopOnExceptions, setWorkDir
-
Constructor Details
-
WebCrawlerConfig
public WebCrawlerConfig()
-
-
Method Details
-
getStartReferencesSitemaps
Gets sitemap URLs to be used as starting points for crawling.- Returns:
- sitemap URLs (never
null) - Since:
- 2.3.0
-
setStartReferencesSitemaps
Sets the sitemap URLs used as starting points for crawling.- Parameters:
startReferencesSitemaps- sitemap URLs- Returns:
- this
- Since:
- 3.0.0
-
getKeepReferencedLinks
Gets what type of referenced links to keep, if any. Those links are URLs extracted by link extractors. See class documentation for more details.- Returns:
- preferences for keeping links
- Since:
- 3.0.0
-
setKeepReferencedLinks
public WebCrawlerConfig setKeepReferencedLinks(Set<WebCrawlerConfig.ReferencedLinkType> keepReferencedLinks) Sets whether to keep referenced links and what to keep. Those links are URLs extracted by link extractors. See class documentation for more details.- Parameters:
keepReferencedLinks- option for keeping links- Returns:
- this
- Since:
- 3.0.0
-
getLinkExtractors
Gets link extractors.- Returns:
- link extractors
-
setLinkExtractors
Sets link extractors.- Parameters:
linkExtractors- link extractors- Returns:
- this
- Since:
- 3.0.0
-
getPostImportLinks
Gets a field matcher used to identify post-import metadata fields holding URLs to consider for crawling.- Returns:
- field matcher
- Since:
- 3.0.0
-
setPostImportLinks
Set a field matcher used to identify post-import metadata fields holding URLs to consider for crawling.- Parameters:
fieldMatcher- field matcher- Returns:
- this
- Since:
- 3.0.0
-
getUrlNormalizers
Gets URL normalizers. Executed in the order provided. Defaults to a singleGenericUrlNormalizerwith its default settings.- Returns:
- URL normalizers
-
setUrlNormalizers
Sets URL normalizers. Executed in the order provided. Defaults to a singleGenericUrlNormalizerwith its default settings.- Parameters:
urlNormalizers- URL normalizers- Returns:
- this
-
getUrlScopeResolver
The strategy to use to determine if a URL is in scope. -
getDelayResolver
The delay resolver dictating the minimum amount of time to wait between web requests. Defaults toGenericDelayResolver. -
getCanonicalLinkDetector
The canonical link detector. Defaults toGenericCanonicalLinkDetector. Set tonullto disable canonical link detection. -
isPostImportLinksKeep
public boolean isPostImportLinksKeep()Whether to keep the Importer-populated fields fromgetPostImportLinks(). By default, those are deleted from a document when the URLs they contain are queued for processing or otherwise evaluated.- See Also:
-
getRobotsTxtProvider
The provider of robots.txt rules for a site (if applicable). Defaults toStandardRobotsTxtProvider. Set tonullto disable. -
getRobotsMetaProvider
The provider of robots metadata rules for a page (if applicable). Defaults toStandardRobotsMetaProvider. Set tonullto disable. -
getSitemapResolver
The resolver of web site sitemaps (if applicable). Defaults toGenericSitemapResolver. Set tonullto disable all sitemap support, or see class documentation to disable sitemap detection only.- See Also:
-
getSitemapLocator
The locator of sitemaps (if applicable). Defaults toGenericSitemapLocator. Set tonullto disable locating sitemaps (relying on sitemaps defined as start reference, if any).- See Also:
-
getRecrawlableResolver
The resolver that indicates whether a given URL is ready to be crawled by a new crawl session. Usually amounts to checking if enough time has passed between two crawl sessions. Defaults toGenericRecrawlableResolver. -
setUrlScopeResolver
The strategy to use to determine if a URL is in scope.- Returns:
this.
-
setDelayResolver
The delay resolver dictating the minimum amount of time to wait between web requests. Defaults toGenericDelayResolver.- Returns:
this.
-
setCanonicalLinkDetector
The canonical link detector. Defaults toGenericCanonicalLinkDetector. Set tonullto disable canonical link detection.- Returns:
this.
-
setPostImportLinksKeep
Whether to keep the Importer-populated fields fromgetPostImportLinks(). By default, those are deleted from a document when the URLs they contain are queued for processing or otherwise evaluated.- Returns:
this.- See Also:
-
setRobotsTxtProvider
The provider of robots.txt rules for a site (if applicable). Defaults toStandardRobotsTxtProvider. Set tonullto disable.- Returns:
this.
-
setRobotsMetaProvider
The provider of robots metadata rules for a page (if applicable). Defaults toStandardRobotsMetaProvider. Set tonullto disable.- Returns:
this.
-
setSitemapResolver
The resolver of web site sitemaps (if applicable). Defaults toGenericSitemapResolver. Set tonullto disable all sitemap support, or see class documentation to disable sitemap detection only.- Returns:
this.- See Also:
-
setSitemapLocator
The locator of sitemaps (if applicable). Defaults toGenericSitemapLocator. Set tonullto disable locating sitemaps (relying on sitemaps defined as start reference, if any).- Returns:
this.- See Also:
-
setRecrawlableResolver
The resolver that indicates whether a given URL is ready to be crawled by a new crawl session. Usually amounts to checking if enough time has passed between two crawl sessions. Defaults toGenericRecrawlableResolver.- Returns:
this.
-
equals
- Overrides:
equalsin classCrawlerConfig
-
canEqual
- Overrides:
canEqualin classCrawlerConfig
-
hashCode
public int hashCode()- Overrides:
hashCodein classCrawlerConfig
-
toString
- Overrides:
toStringin classCrawlerConfig
-